R组条形图

R组条形图,r,bar-chart,R,Bar Chart,我需要一个组条形图,该图如下所示: 使用如下示例数据集: With IT background, No IT background 9,1 2,10 7,1 2,0 我试图用ggplot来做这件事,但无法得到任何可行的解决方案示例数据 Answers <- c(1,10,1,0, 9,2,7,2) IT <- c(rep("With.IT.background",4), rep("No.IT.background",4)) Risks <- c

我需要一个组条形图,该图如下所示:

使用如下示例数据集:

   With IT background, No IT background
   9,1
   2,10
   7,1
   2,0
我试图用
ggplot
来做这件事,但无法得到任何可行的解决方案

示例数据

Answers <- c(1,10,1,0, 9,2,7,2)
IT <- c(rep("With.IT.background",4), rep("No.IT.background",4))
Risks <- c("Aware of risks", "Not aware of risks", "Care about", "Do not care about")
data <- data.frame(cbind(IT, Risks, Answers))

我想你正在寻找下面的代码。我非常同意上面的评论,如果您需要帮助,请始终使您的代码可复制并提供数据。在本例中,我花了一分钟来复制您的数据帧,但假设您有200个值

library(ggplot2)
library(dplyr)
library(reshape2)
#recreate the data frame
data<- data.frame(With.IT.background=c(9,2,7,2), 
              No.IT.background=c(1,10,1,0))
#attach rownames
rownames(data)<- c("Aware of Risks", 
               "Not Aware of Risks", 
               "Care About", "Do Not Care About")
#make rownames a column (dplyr does not send rownames through pipe)
data %>% mutate(Awareness=rownames(data)) -> data

data %>% melt() %>% ggplot(data=., mapping=aes(x=Awareness, 
                               y=value, fill=Awareness)) +
    geom_bar(stat="identity") + theme_bw() + 
    theme(axis.text.x= element_blank(), axis.ticks.x=element_blank())+
    ylab("Answers") + xlab("With IT Background") +
    facet_wrap(~variable) +xlab("")
库(ggplot2)
图书馆(dplyr)
图书馆(E2)
#重新创建数据帧
数据数据
数据%>%melt()%%>%ggplot(数据=,映射=aes(x=感知,
y=值,填充=意识)+
geom_bar(stat=“identity”)+theme_bw()
主题(axis.text.x=element\u blank(),axis.ticks.x=element\u blank())+
ylab(“答案”)+xlab(“有IT背景”)+
镶嵌面_包裹(~变量)+xlab(“”)

下面是一个以R为基数的答案

IT_data  <- data.frame(c(9,2,7,2),
                       c(1,10,1,0))
colnames(IT_data)  <- c("With IT background","No IT background")
rownames(IT_data)  <- c("Aware of Risks","Not Aware of Risks","Care About","Do Not Care About")

barplot(as.matrix(IT_data), beside=TRUE,
        col=rainbow(4),
        legend.text=rownames(IT_data), args.legend=list(x="topleft"))

哪一个更接近OP的要求。

想举一个可复制的例子吗?使用dput()使您的数据集可用,并发布您已经尝试过的内容。+1分享一个可复制的示例。一般来说,您可以使用
facet\u wrap
来完成此操作。我没有任何工作代码,因此发布我所拥有的不会有帮助。我在示例数据中看不到x轴上的变量示例数据是手动生成的,格式现在不重要,我只需要生成上面精确的组条形图,但收到的图是两个带有两个图例的单个图,难道不可能只收到像上面那样的一个图吗?是的,您可以执行面wrapadd库(重塑2),然后在一行数据%>%melt()%%>%ggplot(数据=,映射=aes)中打印(x=意识,y=值,fill=意识))+geom_bar(stat=“identity”)+theme_bw()+theme(axis.text.x=元素_text(hjust=1,角度=45))+ylab(“答案”)+xlab(“有背景”)+facet_wrap(~variable)+xlab(“”)很好的thx它工作了,你能告诉我如何删除x轴上的标签吗,因为它已经和图例中的一样了?顺便说一句,用户1945827的编辑真是太棒了。我想我想我们应该在
ggplot2
中这样做。看起来两个图的比例重叠,在x轴上我得到了一个开始的比例9,7,2,10,1,0??修复并改进了它。
library(ggplot2)
library(dplyr)
library(reshape2)
#recreate the data frame
data<- data.frame(With.IT.background=c(9,2,7,2), 
              No.IT.background=c(1,10,1,0))
#attach rownames
rownames(data)<- c("Aware of Risks", 
               "Not Aware of Risks", 
               "Care About", "Do Not Care About")
#make rownames a column (dplyr does not send rownames through pipe)
data %>% mutate(Awareness=rownames(data)) -> data

data %>% melt() %>% ggplot(data=., mapping=aes(x=Awareness, 
                               y=value, fill=Awareness)) +
    geom_bar(stat="identity") + theme_bw() + 
    theme(axis.text.x= element_blank(), axis.ticks.x=element_blank())+
    ylab("Answers") + xlab("With IT Background") +
    facet_wrap(~variable) +xlab("")
IT_data  <- data.frame(c(9,2,7,2),
                       c(1,10,1,0))
colnames(IT_data)  <- c("With IT background","No IT background")
rownames(IT_data)  <- c("Aware of Risks","Not Aware of Risks","Care About","Do Not Care About")

barplot(as.matrix(IT_data), beside=TRUE,
        col=rainbow(4),
        legend.text=rownames(IT_data), args.legend=list(x="topleft"))
barplot(as.matrix(IT_data), beside=TRUE,
        col=rainbow(4),
        legend.text=rownames(IT_data), args.legend=list(x="center",cex=0.7,bty="n"))