Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

r - ggplot2 - create a barplot for every column of a dataframe

I know this question is really basic, but I am a total beginner and I have been the whole day trying to plot individual graphs for every single column of a data frame. Any help would be very useful

Here is the data:

> dfslices
        X0035.A061 X0094.B116 X0314.A038
verylow   19.48052   8.127208 36.8243243
low        2.96846   9.069494  7.4324324
medium     0.00000   2.237927  0.3378378
high       0.00000   0.000000  1.6891892

Basically, I need 1 barplot for each column (X0035.A061, X0094.B116 and X0314.A038). Each barplot has 4 bars (one bar correponding to the very low category, another to the low, another to the medium and another to the high). And it would be great the title of the graphs are (X0035.A061, X0094.B116 and X0314.A038) and every bar of the plot have the corresponding label (verylow, low, medium, and high)

Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here is one solution to the problem using the data.table package for the melt() and fread() functions, and using facet_grid() from ggplot2 to plot all 3 of your original columns as separate panels on a single plot.

library(data.table)
library(ggplot2)

# Convert text data to data.table using fread() from the data.table package.
dfslice = fread("category X0035.A061 X0094.B116 X0314.A038
                 verylow    19.48052   8.127208 36.8243243
                 low         2.96846   9.069494  7.4324324
                 medium      0.00000   2.237927  0.3378378
                 high        0.00000   0.000000  1.6891892")

# Convert data to 'long form' using melt() from the data.table package.
mtab = melt(dfslice, id.vars="category")

# Manually set factor levels of 'category' column to plot in a logical order.
mtab$category = factor(mtab$category, 
                       levels=c("verylow", "low", "medium", "high"))

mtab
#     category   variable      value
#  1:  verylow X0035.A061 19.4805200
#  2:      low X0035.A061  2.9684600
#  3:   medium X0035.A061  0.0000000
#  4:     high X0035.A061  0.0000000
#  5:  verylow X0094.B116  8.1272080
#  6:      low X0094.B116  9.0694940
#  7:   medium X0094.B116  2.2379270
#  8:     high X0094.B116  0.0000000
#  9:  verylow X0314.A038 36.8243243
# 10:      low X0314.A038  7.4324324
# 11:   medium X0314.A038  0.3378378
# 12:     high X0314.A038  1.6891892

p = ggplot(data=mtab, aes(x=category, y=value, fill=category)) +
    geom_bar(stat="identity") +
    scale_fill_viridis_d() +
    facet_grid(. ~ variable)

ggsave("faceted_barplot.png", plot=p, width=7.5, height=2.5, dpi=150)

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...