简体   繁体   中英

ggplot2: geom_line() for single observations (x-factor, y-numeric)

I have the following data and simple code

library(ggplot2)
dane <- data.frame(mylevels=c(1,2,5,9), myvalues=c(2, 5, 3, 4))
ggplot(dane, aes(x=factor(mylevels), y=myvalues)) + geom_line() + geom_point(size=3)

I'm not able to figure out how to force "ggplot2" to draw the line - I get an error. On pp. 55 (R Graphics Cookbook) Winston Chang describes the same error but my plot is simpler that's why his solution can't be adopted.

You should add group=1 inside aes() to connect points with line.

ggplot(dane, aes(x=factor(mylevels), y=myvalues,group=1)) + 
       geom_line() + geom_point(size=3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM