0
votes

After some search about my topic without a satisfing answer or one I can adapt to my data, here one more question about plotting a grouped (or stacked) barplot with either ggplot2 or barplot.

I have the following table:

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

I would like to creat a grouped barplot for each ID (1-11) with the values of the mean, med, max and min. The Lake size is kind of irrelevant here.

So far I tried to help myself with this: https://www.theanalysisfactor.com/r-11-bar-charts/ and Grouped barplot in ggplot2 in R

One of my tries was this:

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

The y axis should show the vales (about 0 to 15), the x axis the grouped Min, max, med, mean for Lakes 1 to 11 each.

Would be great if someone can provide some help. Thanks!

2

2 Answers

0
votes

Put the data into a csv file named "test.csv" like this:

    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

Then write the code below:

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

The plot:

plot.jpg

Hope this helps!

0
votes

Here is a possible solution using R basic plotting functions:

1/ from the data of your example:

    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/ select the columns you want and (re)set column and row names

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

3/ then, plot the data:

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)

This can be easily customized to your needs. Hope it helps.