![]() ![]() All objects will be fortified to produce a data frame. If NULL, the default, the data is inherited from the plotĭata as specified in the call to ggplot().Ī ame, or other object, will override the plotĭata. You must supply mapping if there is no plot Inherit.aes = TRUE (the default), it is combined with the default mappingĪt the top level of the plot. Other methods to reshape the data are described here: Reshaping ame from wide to long format.Set of aesthetic mappings created by aes(). the function 'melt' from reshape2 package. But first we need to melt the data from wide to long format, using e.g. In this case, we can use the colour aessthetics, which matches colour of the lines to different levels of a variable in the data set (here: y1 vs y2). ![]() Then, by using different 'grouping variables' in the aesthetics arguments, properties of the line, such as linetype or colour, will vary according to the grouping variable, and corresponding legends will appear. ![]() However, when using ggplot it is generally most convenient to keep the data in a data frame in a 'long' format. In that answer the data were in a 'wide' format. So curve is the tool meant to handle functions.Īs described by you may plot the two lines in the same graphical device using ggplot. When you call plot(sin) R figures out that sin is a function (not y values) and uses the plot.function method, which ends up calling curve. Here's the result of calling plot(0:2) curve(sin).īehind the scenes, check out methods(plot). lines plots points with x and y values, like: lines( x=0:10, y=sin(0:10) ).Īnd here's a minor difference: curve needs to be called with add=TRUE for what you're trying to do, while lines already assumes you're adding to an existing plot. (If you can't remember that these are the names of the two important plotting commands, just sing it.) Here's the big difference between curve and lines.Ĭurve will plot a function, like curve(sin). What always confused me about plotting is the difference between curve and lines. Look how messed up the vertical axis labels are! Since the ranges are different you would need to set ylim=c(lowest point between the two functions, highest point between the two functions), which is less easy than what I'm about to show you-and way less easy if you want to add not just two curves, but many. The output of plot(sin) par(new=T) plot( function(x) x**2 ). I disagree with par(new=TRUE) because that will double-print tick-marks and axis labels. Tl dr: You want to use curve (with add=TRUE) or lines. You can refer to this answer in order to see the corresponding code. For example, we can add labels for axises: g <- g + ylab("Y") + xlab("X")Īs pointed out in comments, ggplot's philosophy suggests using data in long format. Further enchantments of the plot are also made with created object. G produces the plot, and you can see it at every stage (well, after creation of at least one layer). G <- g + geom_line(aes(y=y2), colour="green") ![]() Say, usual step-by-step setup can look like this: g <- ggplot(df, aes(x)) With ggplot you have access to graphical object on every stage of plotting. Here + operator is used to add extra layers to basic object. Geom_line(aes(y=y2), colour="green") # second layer Geom_line(aes(y=y1), colour="red") + # first layer Ggplot(df, aes(x)) + # basic graphical object Ggplot style requires data to be packed in ame. The idea is to create a graphical object with basic aesthetics and enhance it incrementally. When constructing multilayer plots one should consider ggplot package. ![]()
0 Comments
Leave a Reply. |