【发布时间】:2020-06-23 04:42:23
【问题描述】:
给定一个数据框如下:
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,但我想为负值和正值设置不同的颜色。
如何在 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)
【问题讨论】: