【问题标题】:R group bar plotR组条形图
【发布时间】:2016-04-08 03:10:46
【问题描述】:

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

使用这样的示例数据集:

   With IT background, No IT background
   9,1
   2,10
   7,1
   2,0

我尝试使用 ggplot 进行此操作,但找不到任何可行的解决方案

【问题讨论】:

  • 想制作一个可重现的例子吗?使用 dput() 使您的数据集可用并发布您已经尝试过的内容。
  • +1 分享一个可重现的例子。一般来说,您可以使用facet_wrap link 来完成此操作。
  • 我没有任何工作代码,因此发布我所拥有的内容无济于事
  • 我在您的示例数据中没有看到 x 轴上的变量
  • 示例数据是手动生成的,格式现在不重要,我只需要生成与上面完全相同的组条形图

标签: r bar-chart


【解决方案1】:

这是基于 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"))

图例的位置和大小可以用下面的代码改进,

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"))

这更接近 OP 的要求。

【讨论】:

    【解决方案2】:

    我认为您正在寻找下面的代码。我非常同意上述评论者的观点,如果您寻求帮助,请始终使您的代码可重现并提供数据。在这种情况下,我花了一分钟来复制您的数据框,但假设您有 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("")
    

    【讨论】:

    • 但是收到的地块是两个单独的地块有两个图例,是不是像上面的一样只能收到一个地块?
    • 是的,你可以做 facet wrap
    • 添加库(reshape2) 然后在一行中绘制数据 %>% melt() %>% ggplot(data=., mapping=aes(x=Awareness, y=value, fill=Awareness )) + geom_bar(stat="identity") + theme_bw() + theme(axis.text.x= element_text(hjust=1, angle=45))+ ylab("Answers") + xlab("有IT背景" ) + facet_wrap(~变量) +xlab("")
    • 非常感谢它的工作,你能否告诉我如何删除 x 轴上的标签,因为它已经与图例中的相同?
    • 顺便说一句,user1945827 的编辑真的很棒。我想我假设我们要在 ggplot2 中执行此操作。
    【解决方案3】:

    示例数据

    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))
    

    更改列类型

    data$Answers <- as.numeric(as.character(data$Answers))
    data$Risks <- factor(data$Risks, levels = c("Aware of risks","Not aware of risks", "Care about", "Do not care about"))
    

    还有情节

    library(ggplot2)
    ggplot(data, aes(x=factor(Risks), y=Answers, fill=factor(Risks))) + 
          geom_bar(stat="identity") + facet_grid(. ~ IT) + 
          ggtitle("Awareness and risks") + guides(fill=guide_legend(title=NULL)) +
          labs(x = "") + theme(axis.text.x = element_blank()) + # remove xlab and tickers
          theme(legend.position=c(.5,.7)) +          # put the legend inside
          theme(legend.background=element_blank()) + # Remove overall border
          theme(legend.key=element_blank())          # Remove border around each item
    

    【讨论】:

    • 看起来这两个图的比例是重叠的,在 x 轴上我得到一个从 9,7,2,10,1,0 开始的比例??
    • 修复并改进。
    猜你喜欢
    • 1970-01-01
    • 2013-02-11
    • 2017-01-02
    • 2021-04-22
    • 2016-07-11
    • 1970-01-01
    • 2021-10-01
    • 2016-11-30
    • 1970-01-01
    相关资源
    最近更新 更多