ggplot2-为数据框的每一列创建一个barplot [英] ggplot2 - create a barplot for every column of a dataframe

查看:126
本文介绍了ggplot2-为数据框的每一列创建一个barplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题确实很基本,但是我是一个初学者,我整天都在尝试为数据帧的每一列绘制单独的图。任何帮助都将非常有用

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

以下是数据:

> 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

基本上,我需要为每列(X0035.A061,X0094.B116和X0314.A038)绘制1个小图。每个条形图都有4条(一条对应于非常低的类别,另一条对应于低,另一条对应于中,另一条对应于高)。
图表的标题是(X0035.A061,X0094.B116和X0314.A038),并且图表的每个小节都有相应的标签(非常低,低,中和高),将非常棒

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)

谢谢

推荐答案

以下是使用 data.table 包用于 melt() fread()函数,并使用 ggplot2 中的 facet_grid()将所有3个原始列绘制为一个单独的面板

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)

这篇关于ggplot2-为数据框的每一列创建一个barplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆