(更多关于图例、坐标系等内容请见:R、ggplot2、shiny 汇总)
library(ggplot2)
library(ggthemes)
dt = data.frame(obj = c(‘A‘,‘D‘,‘B‘,‘E‘,‘C‘), val = c(2,15,6,9,7))
p = ggplot(dt, aes(x = obj, y = val, fill = obj, group = factor(1))) +
geom_bar(stat = "identity") +
theme_economist()
p
library(ggplot2)
library(ggthemes)
dt = data.frame(obj = c(‘A‘,‘D‘,‘B‘,‘E‘,‘C‘), val = c(2,15,6,9,7))
p = ggplot(dt, aes(x = obj, y = val, fill = obj, group = factor(1))) +
geom_bar(stat = "identity", width = 0.5) + ## 修改柱条的宽度
theme_economist()
p
library(ggplot2)
library(ggthemes)
dt = data.frame(obj = c(‘A‘,‘D‘,‘B‘,‘E‘,‘C‘), val = c(2,15,6,9,7))
dt$obj = factor(dt$obj, levels=c(‘D‘,‘B‘,‘C‘,‘A‘,‘E‘)) ## 设置柱条的顺序
p = ggplot(dt, aes(x = obj, y = val, fill = obj, group = factor(1))) +
geom_bar(stat = "identity", width = 0.5) + ## 修改柱条的宽度
theme_economist()
p
## 特注:dt$obj 是因子类型,ggplot2作图的顺序就是按照这个因子水平的顺序来的,
## 所以我们修改因子水平的顺序即可修改作图的顺序,具体情况可以输出一下 dt$obj 。
library(ggplot2)
library(ggthemes)
dt = data.frame(obj = c(‘A‘,‘D‘,‘B‘,‘E‘,‘C‘), val = c(2,15,6,9,7))
dt$obj = factor(dt$obj, levels=c(‘D‘,‘B‘,‘C‘,‘A‘,‘E‘)) ## 设置柱条的顺序
p = ggplot(dt, aes(x = obj, y = val, fill = obj, group = factor(1))) +
geom_bar(stat = "identity", width = 0.5) + ## 修改柱条的宽度
theme_economist() +
geom_text(aes(label = val, vjust = -0.8, hjust = 0.5, color = obj), show_guide = FALSE) + ## 显示柱条上的数字
ylim(min(dt$val, 0)*1.1, max(dt$val)*1.1) ## 加大 Y 轴的范围,防止数字显示不齐全
p
转载请注明出处,谢谢!(原文链接:http://blog.csdn.net/bone_ace/article/details/47267981)
原文地址:http://blog.csdn.net/bone_ace/article/details/47267981