【问题标题】:grouped barplot from table表格中的分组条形图
【发布时间】:2019-03-28 22:42:48
【问题描述】:

在对我的主题进行了一些搜索后,没有一个令人满意的答案或者我可以适应我的数据,这里还有一个关于使用 ggplot2 或 barplot 绘制分组(或堆叠)条形图的问题。

我有下表:

Table_lakes
    Lake Size Lake Mean Lake Med  Lake Max  Lake Min
 1:   2419723  6.557441 6.562879  9.107328 4.7520108
 2:    737345  1.569643 1.562833  2.643082 0.9065250
 3:   1904419  3.006871 2.989362  4.100533 2.3644874
 4:    633220  3.170494 3.154871  4.580919 1.6915103
 5:   3417157  4.587906 4.589763  5.865326 3.5397623
 6:   3046643  1.784759 1.783092  2.921241 0.6835220
 7:   3868608  2.152185 2.188566  5.382725 0.1158626
 8:  11952064  9.391443 9.342757 12.524334 8.5829620
 9:   2431961  7.796330 7.833883  9.186878 5.9242287
10:   5624563  8.444996 8.482042 12.207799 7.3909297
11:   2430490  3.474408 3.438787  5.186004 2.3032870

我想为每个 ID (1-11) 创建一个分组条形图,其中包含平均值、中值、最大值和最小值。湖的大小在这里有点无关紧要。

到目前为止,我试图帮助自己解决这个问题:https://www.theanalysisfactor.com/r-11-bar-charts/Grouped barplot in ggplot2 in R

我的一个尝试是这样的:

 ggplot(Table_lakes[Table_lakes$`Lake Mean` & Table_lakes$`Lake Max`],
       aes(x = factor(Name), y = Table_lakes)) + 
  geom_bar(stat = "identity", position="dodge") +
  labs(x = "Name", y = "Height")

y 轴应显示 vales(大约 0 到 15),x 轴应显示分组的最小值、最大值、中值、平均值,分别用于 1 到 11 个湖。

如果有人可以提供一些帮助,那就太好了。谢谢!

【问题讨论】:

    标签: r ggplot2 bar-chart


    【解决方案1】:

    这是使用 R 基本绘图功能的可能解决方案:

    1/ 来自您的示例数据:

        RAWDATA = "ID    Lake_Size Lake_Mean Lake_Med  Lake_Max  Lake_Min
     1:   2419723  6.557441 6.562879  9.107328 4.7520108
     2:    737345  1.569643 1.562833  2.643082 0.9065250
     3:   1904419  3.006871 2.989362  4.100533 2.3644874
     4:    633220  3.170494 3.154871  4.580919 1.6915103
     5:   3417157  4.587906 4.589763  5.865326 3.5397623
     6:   3046643  1.784759 1.783092  2.921241 0.6835220
     7:   3868608  2.152185 2.188566  5.382725 0.1158626
     8:  11952064  9.391443 9.342757 12.524334 8.5829620
     9:   2431961  7.796330 7.833883  9.186878 5.9242287
    10:   5624563  8.444996 8.482042 12.207799 7.3909297
    11:   2430490  3.474408 3.438787  5.186004 2.3032870"
    
    DATA = read.table(textConnection(RAWDATA), header=TRUE)
    

    2/ 选择您想要的列并(重新)设置列名和行名

    A  = DATA[, 3:6]
    rownames(A) = paste0("#", 1:nrow(A))
    colnames(A) = c("Mean", "Median", "Max", "Min")
    

    3/ 然后,绘制数据:

    cols = c("blue", "darkblue",  "red", "green") # bar colors
    mainsep = 0.1 # space between grouped bars
    secsep = 0 # space between bars
    
    # defining an empty plot with the right dimensions
    xlim = c(0, nrow(A)-mainsep)
    ylim = c(0, max(A))
    plot(NA, xlim=xlim, ylim=ylim, xaxt="n", ylab="Lake level [m]", xlab="Lakes ID")
    # create the x-axis with the table row names as labels
    axis(1, at=1:nrow(A)-0.5-mainsep/2, labels=rownames(A), tick=FALSE, mgp=c(3, 0.1, 0))
    axis(1, at=0:nrow(A)-mainsep/2, labels=NA, tick=TRUE)
    # create the grouped bar according to the column of the table
    boxsize = (1-mainsep)/ncol(A)
    for (i in 1:nrow(A)) {
        for (j in 1:ncol(A)) {
            rect((i-1)+boxsize*(j-1), 0, (i-1)+boxsize*j-secsep, A[i, j], col=cols[j])
        }
    }
    # add a legend to identify the content of each column
    legend("top",  horiz=TRUE, legend=colnames(A), col="black", pt.bg=cols, pch=22, pt.cex=2)
    

    这可以根据您的需要轻松定制。希望对您有所帮助。

    【讨论】:

    • 实际上,从您提供的链接 (https://www.theanalysisfactor.com/r-11-bar-charts/) 中,您会发现使用 barplot() 的解决方案要简单得多:barplot(as.matrix(t(A)), main="", ylab="Lake level [m]", beside=TRUE, col=cols)
    • 非常感谢!实际上,当我使用相同的脚本时,我只是得到一个空图,当然,它会根据我的数据进行相应的更改。不知道是什么问题。
    • 我还用您在评论中复制的链接再次尝试了它。在那里我总是得到错误的组(最大,最小,..),而不是相同 ID 的组。因此,我尝试通过根据 id 转置表格来提供帮助,但它仍然为 max、min、...groups 而不是每个 id 绘制。你知道为什么吗?
    • 我不知道为什么转置表格不起作用。这应该。你是表一个data.frame,一个矩阵吗?使用名为“Table_lakes”的表,以下应该可以工作:barplot(t(as.matrix(Table_lakes[, c("Lake_Mean", "Lake_Med", "Lake_Max", "Lake_Min")])), main="", ylab="Lake level [m]", beside=TRUE, col=c("lightblue", "darkblue", "red", "green"))注意,Table_lakes 的行名被视为 x 轴上组的标签。
    • 非常感谢伊万!您的新评论非常有效! =)
    【解决方案2】:

    将数据放入名为“test.csv”的 csv 文件中,如下所示:

        id  size    mean    med max min
    1   2419723 6.557441    6.562879    9.107328    4.7520108
    2   737345  1.569643    1.562833    2.643082    0.906525
    3   1904419 3.006871    2.989362    4.100533    2.3644874
    4   633220  3.170494    3.154871    4.580919    1.6915103
    5   3417157 4.587906    4.589763    5.865326    3.5397623
    6   3046643 1.784759    1.783092    2.921241    0.683522
    7   3868608 2.152185    2.188566    5.382725    0.1158626
    8   11952064    9.391443    9.342757    12.524334   8.582962
    9   2431961 7.79633 7.833883    9.186878    5.9242287
    10  5624563 8.444996    8.482042    12.207799   7.3909297
    11  2430490 3.474408    3.438787    5.186004    2.303287
    

    然后写下面的代码:

    require(reshape2)
    #the reshape2 package is required here
    testData <- read.csv('~/Desktop/test.csv')
    testData$id <- as.factor(testData$id) 
    #convert the id field to factors instead of numerical values
    testDataMelt <- reshape2::melt(testData, id.vars = "id", value.name = "value")
    testDataMelt <- testDataMelt[testDataMelt$variable != "size",]
    #convert the data to the format that is convenient for plotting by ggplot2 and remove the size field
    ggplot(testDataMelt, aes(x = id, y = value, group = variable, fill = variable)) + geom_bar(stat = "identity", position = "dodge")
    #finally, ggplot the data
    

    剧情:

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-29
      • 1970-01-01
      • 2011-03-01
      相关资源
      最近更新 更多