给定一个数据帧,如下所示:
df <- data.frame(city = c("bj", "sh", "gz", "sz"),
price = c(12, 7, 5, 6),
pct = c(-2.3, 5, -4, 4), stringsAsFactors=FALSE)
出:
city price pct
0 bj 12 -2.3
1 sh 7 5.0
2 gz 5 -4.0
3 sz 6 4.0
我想画一个情节与ggplot:barchart
对city
,point
对pct
,但我想消极和积极的价值观pct
有不同的颜色。
如何在ggplot2中做到这一点?谢谢。
码:
ggplot(df, aes(fill = city, y = price, x = city)) +
geom_bar(position = "dodge", stat = "identity", alpha = 0.5, fill = "#FF6666") +
geom_point(data = df, aes(x = 'city', y = 'pct'), size = 2)
您可以将其pct>0
用作颜色(0或1,取决于的符号pct
)并进行以下转换city
:
ggplot(df, aes(fill = city, y = price, x = city)) +
geom_bar(position = "dodge", stat = "identity", alpha = 0.5, fill = "#FF6666") +
geom_point(data = df, aes(x = factor(city), y = pct ,color = pct>0, size = 2))
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句