In: Computer Science
qplot(displ, hwy, data = mpg, geom = c("point", "smooth"))
qplot(color, data = diamonds, fill = cut, position = "identity")
The solution for the above problem is given below and if you feel any problem then feel free to ask:
qplot(displ, hwy, data = mpg, geom = c("point", "smooth"))
Here, the displ is for the x-axis and hwy is for the y-axis which are the aesthetics for the layer, mpg is the data frame to use and geom is setting the behavior of the plot to draw as smooth points.
So, in ggplot2 the qplot is converted as below:
ggplot(mpg, aes(displ, hwy)) + geom_point() + geom_smooth()
Here, the data frame is given first and aesthetics are given in aes() function also the geoms are given separately as point and smooth.
Now for the second plot statement:
qplot(color, data = diamonds, fill = cut, position = "identity")
Here, the only x is supplied to qplot as color therefore the resulting in formation of bars or histogram, diamonds is the data frame to use, fill is used to control the color areas of histogram which is set to cut to fill the histogram bars based on the cut and the position is used for overlapping or proper formatting of colors which is given identity but this position gets deprecated in case of qplot.
So, in ggplot2 the qplot statement is converted as given below:
ggplot(diamonds, aes(color, fill = cut)) + geom_bar(position = "stack")
Here, the aesthetics are passed as color which is x for the graph and fill = cut and in addition the geom_bar() function is passed with position="stack" as in ggplot you have to mention the shape of the plot and in case of position ="identity" all the colors get overlapped by the one color which is not going to happen in qplot so you have to use position="stack" so that all colors can appear as required but if you want to use position="identity" then you can simply replace the stack by identity but for same functioning as qplot use stack in position.