使用ggplot2在R上分组条形图
如何使用此数据在R上使用使用ggplot2在R上分组条形图,r,ggplot2,geom-bar,R,Ggplot2,Geom Bar,如何使用此数据在R上使用ggplot2创建分组条形图 Person Cats Dogs Mr. A 3 1 Mr. B 4 2 所以它显示了每个人拥有的宠物数量,用这个布局 我有一个包含这些数据的文本文件,并使用read.delim读取R上的文件 我已经使用了这个代码,但它不会产生我正在寻找的条形图 ggplot(data=pets, aes(x=Person, y=Cats, fill=Dogs)) + geom_bar(stat="identity", position
ggplot2
创建分组条形图
Person Cats Dogs
Mr. A 3 1
Mr. B 4 2
所以它显示了每个人拥有的宠物数量,用这个布局
我有一个包含这些数据的文本文件,并使用read.delim
读取R上的文件
我已经使用了这个代码,但它不会产生我正在寻找的条形图
ggplot(data=pets, aes(x=Person, y=Cats, fill=Dogs)) + geom_bar(stat="identity", position=position_dodge())
我是R的新手,任何帮助都将不胜感激
提前谢谢 要准备分组条形图的数据,请使用
重塑2
软件包的melt()
功能
I.装载所需的包裹
library(reshape2)
library(ggplot2)
二,。创建数据帧df
df <- data.frame(Person = c("Mr.A","Mr.B"), Cats = c(3,4), Dogs = c(1,2))
df
# Person Cats Dogs
# 1 Mr.A 3 1
# 2 Mr.B 4 2
四、 按人员分组的条形图
ggplot(data.m, aes(Person, value)) + geom_bar(aes(fill = variable),
width = 0.4, position = position_dodge(width=0.5), stat="identity") +
theme(legend.position="top", legend.title =
element_blank(),axis.title.x=element_blank(),
axis.title.y=element_blank())
顶部图例、删除图例标题、删除轴标题、调整条宽和条间距
到目前为止,我在网上找到的最好的解释是
ggplot(data.m, aes(Person, value)) + geom_bar(aes(fill = variable),
width = 0.4, position = position_dodge(width=0.5), stat="identity") +
theme(legend.position="top", legend.title =
element_blank(),axis.title.x=element_blank(),
axis.title.y=element_blank())