I’ve got a list and some data
l <- c('b', 'a')
frame <- data.frame(series = rep(c('a', 'b'), 6), x = c(1:12), y = c(1:12))
and I’d like to plot a smoothed line via
ggplot(frame, data) + geom_smooth(linetype=series)
But I need the linetype based on the list l, the first element in the list l should be
normal, the second element of the list dotted. I tried
ggplot(frame, data) + geom_smooth() + scale_linetype_manual("", values=series, breaks=l)
But that doesn’t apply any linetype.
You example data is a little odd, in that you have a grouping variable (series) and one numerical column (data), but it sounds like you want to plot two variables. Here’s some possibly more relavent example data:
Note the use of
=rather than<-. Had you noticed that the column names of your data frame were unspeakably ugly? 😉 Also note that I didn’t use the worddata, as that can get confusing as it is used as a function, and oftentimes an argument.Then you could plot two lines like this:
Or two smoothed lines like this (with copious warnings thrown due to the small size of the data):
The key here is that you pass
ggplota data frame (frame) and then map variables to aesthetics using theaes()function. In this case, we’ve mapped the x,y values to our x,y variables, and mapped linetype to series. But we have to tell ggplot how to group the data, hence the use of the group aesthetic.Aesthetics can be mapped in
ggplotin which case they carry forward to subsequent geoms, or they can be mapped in only the geom in which they are used.Finally, to specify which line types to use, you were correct in trying
scale_linetype_manual:where you pass to the
valuesargument the linetypes you want used. in the scale. You can also pass a named vector tovalues, so specify which levels get which line types: