简体   繁体   中英

Independently scale geom_line and geom_point in ggplot2

Here is a short example:

> V1 = c('a','a','b','b') # categories
> V2 = c(1,3,4,2) # y values
> V3 = c(1,2,1,2) # x values
> data.frame(V1,V2,V3)
  V1 V2 V3
1  a  1  1
2  a  3  2
3  b  4  1
4  b  2  2

> ggplot(data.frame(V1,V2,V3), aes(x=V3, y=V2, size=V1)) + geom_point() + geom_line()

不同的线号

However, I would like to link the two points in the same category (as in the plot) ALWAYS using a line of size 1, instead of also scaling the line size AS WELL AS the point size. In other words, I want to scale the ONLY the point size but NOT the lines. I tried this:

> ggplot(data.frame(V1,V2,V3), aes(x=V3, y=V2, size=V1)) + geom_point() + geom_line(size=1)

But got this: 相同的线号

Set the grouping, then the aesthetics for the points separately.

ggplot(data.frame(V1,V2,V3), aes(x=V3, y=V2, group=V1)) + geom_point(aes(size = V1)) + geom_line()

在此处输入图片说明

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