我有一个数据框,其中包含2组的“ Y”或“ N”列,例如:
drug<-c("Y","Y","N","Y","Y","Y","N","N","N","N","N","Y","Y","Y","N","N")
group<-c(0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1)
df<-data.frame(drug,group)
我想为两个组的“ Y” /“ N”设置条形图,两个组彼此相邻。 我用ggbarplot尝试了各种方法,并得到了奇怪的图
ggbarplot(my_matches, x = "group", y = "drug",
color = "group", palette = c("#00AFBB", "#FC4E07"))
并尝试制作表格并将其绘制为类似barplot的
counts0<-df[which(df$group==0),]
counts1<-df[which(df$group==1),]
grp0<-table(counts0$drug)
grp1<-table(counts1$drug)
s<- as.data.frame(t(rbind(grp0,grp1)))
barplot(s$grp0, s$grp1,beside=T)
您可以说,我是一个初学者,一直在疯狂地尝试解决这个问题。请帮忙!
答案 0 :(得分:3)
首先,不需要将向量创建为数据帧列,并且df
不是很好的变量名(有一个同名的函数)。像这样一步创建您的数据框:
mydata <- data.frame(drug = c("Y","Y","N","Y","Y","Y","N","N","N","N","N","Y","Y","Y","N","N"),
group = c(0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1))
第二:如果您正在使用数据帧,则值得学习dplyr。因此,将其与ggplot2
一起安装,然后加载:
library(dplyr)
library(ggplot2)
现在我们可以按组计算是/否:
mydata %>%
count(group, drug)
# A tibble: 4 x 3
group drug n
<dbl> <fct> <int>
1 0 N 3
2 0 Y 5
3 1 N 5
4 1 Y 3
然后绘制计数与组的关系。我们需要将组转换为因子,因为组是一个分类变量:
mydata %>%
count(group, drug) %>%
mutate(group = factor(group)) %>%
ggplot(aes(group, n)) +
geom_col(aes(fill = drug))