给定一个数据框如下:
df <- data.frame(city = c("bj", "sh", "gz", "sz"),
price = c(12, 7, 5, 6),
pct = c(-2.3, 5, -4, 4), stringsAsFactors=FALSE)
Out:
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
for city
, point
for pct
,但我想为负值和正值使用不同的颜色。
我怎样才能在ggplot2中做到这一点?
Code:
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)