【问题标题】:r grouped barplot from Excel CSV filer 来自 Excel CSV 文件的分组条形图
【发布时间】:2013-09-09 01:12:54
【问题描述】:

我正在尝试在 r 中制作分组条形图,但有些事情我无法弄清楚。这是我目前所拥有的:

我想要:

  1. 从 data.frame(.csv 文件,见下文)创建矩阵
  2. ablines 出现,但不在栏的前面
  3. 分组条形图的标签(11 月、12 月、1 月……->请参阅下面的数据)
  4. 绘图布局如下所示。 (我基本上想要情节边框)

我使用了以下代码:

x<-matrix(nrow=3,ncol=7, data=c(200,227,196,210,279,319,220,126,111,230,196,123,240,106,94,250,154,233,260,226,218))
tiff("p_month_all.tiff", width=600, height=300)
par(mar=c(5,4,0.5,0.5))
a=c("November","December","January","February","March","April","May")
barplot(x, beside=TRUE, ylim=c(0,350),xlab="Month", axes=TRUE,axis.lty=1, ylab="Monthly Precipitation [mm]", col=c("darkblue","dodgerblue3","deepskyblue1"),panel.first= abline(h = c(50,100,150,200,250,300), col = "grey", lty = 2), xaxt="n", yaxt="n")
par(ps=12, cex =1, cex.main=2)
axis(2, c(0,350, c(50, 100, 150, 200, 250, 300)), las=1)
dev.off()

数据集(.csv 文件)如下所示:

Month      Hornberg   Strick   Huetten
November     120       278       234
December     279       156       145
January      328       300       299
February     267       259       234
March        190       201       187
April        150       199       177
May          147       156       160

【问题讨论】:

  • 不清楚你想要什么第一点(“从data.frame创建矩阵”)。您是否在从 CSV 导入数据或将数据框转换为矩阵时遇到问题?后者可以用as.matrix解决。其他问题是关于绘制情节的,所以我认为这需要在一个单独的问题中。
  • 从 .csv 导入数据不是问题,但我想创建一个矩阵,其中包含图表标签的行名和列名。

标签: r matrix ggplot2 dataframe bar-chart


【解决方案1】:

所以,首先,看一下ggplot2文档,还不错http://docs.ggplot2.org/0.9.3.1/index.html 如果您还没有找到问题的答案,请不要放弃谷歌搜索:)

好的,关于你的问题:

  1. 创建数据

help(read.csv) -> 将数据导入到名为 x 的 data.frame 为绘图准备数据:

融化您的数据以将其用于绘图

    x<-melt(x)
  1. 使用月份变量作为因子并按月份排序:

    x$Month=factor(x$Month,level=month.name) 
    x<-x[order(x$Month),]
    
  2. 使用 ggplot2 绘制图形(正如您在此处标记的那样,它在使用中很简单)

        ggplot(x,aes(x=Month,y=value,fill=variable))+geom_bar(stat="bin",position="dodge")+theme_bw()+ylab("Monthly Precipitation [mm]")+xlab("Month")
    

对于颜色,可以使用 scale_fill_brewer() (这里有很好的教程:http://www.cookbook-r.com/Graphs/Colors_%28ggplot2%29/

ggplot(x,aes(x=Month,y=value,fill=variable))+geom_bar(stat="bin",position="dodge")+theme_bw()+ylab("Monthly Precipitation [mm]")+xlab("Month")+scale_fill_brewer(palette="Blues")

【讨论】:

    【解决方案2】:

    为了清楚起见,我已经重写了您的代码,以便您更容易看到问题所在。

    您使用xaxt = "n"yaxt = "n" 抑制了坐标轴。我删除了那些行。 添加对box 的调用会在绘图周围绘制框。 添加对grid 的调用会在图中绘制网格线。 我已将行名和列名添加到您的数据矩阵中,以便绘图知道在轴中使用什么。 我已经更新了情节边距。 我还整理了一些内容,例如将月份名称替换为 month.name 并使用 seq.int 而不是硬编码序列。

    x <- matrix(
      c(
        200, 227, 196, 
        210, 279, 319, 
        220, 126, 111,
        230, 196, 123,
        240, 106, 94,
        250, 154, 233,
        260, 226, 218
      ),
      nrow = 3,
      ncol = 7
    )
    colnames(x) <- month.name[c(11:12, 1:5)]
    rownames(x) <- c("Hornberg", "Strick", "Huetten")
    
    
    par(mar = c(5, 4, 1.5, 0.5), ps = 12, cex  = 1, cex.main = 2, las = 1)
    
    barplot(
      x, 
      beside      = TRUE, 
      ylim        = c(0,350),
      xlab        = "Month", 
      axes        = TRUE,
      axis.lty    = 1, 
      ylab        = "Monthly Precipitation [mm]",
      col         = c("darkblue", "dodgerblue3", "deepskyblue1"),
      panel.first =  abline(
        h    =  seq.int(50, 300, 50), 
        col  =  "grey", 
        lty  =  2
      )
    )
    box()
    grid()
    

    【讨论】:

    • @samjam 如果你觉得这个答案有用,你可以点击左边的向上箭头来投票。您可以这样做以获得多个答案。
    猜你喜欢
    • 2020-12-22
    • 2017-01-02
    • 1970-01-01
    • 2017-01-03
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多