【问题标题】:Barplots from data frame R [duplicate]来自数据框 R 的条形图 [重复]
【发布时间】:2018-04-13 04:38:51
【问题描述】:

我是 R 的初学者,不熟悉语法。您的帮助将不胜感激。

我有一个 3 列 27 行的数据框。

Gene_Name Cell_line1 Cell_line2
gene1        23          45
gene2        0.6         21
gene3        34          18
---
gene27       45          23

我想制作分组条形图,其中 X 轴上有基因名称,Y 轴上有不同颜色的 cell_line1 和 Cell_line2 值。

我试图用 ggplot2 来实现它

 p<- ggplot(data=df, aes(Gene_Names, Cell_line1)) + geom_bar(stat="identity", position = "dodge")+ theme(axis.text.x = element_text(angle = 90, hjust = 1))

您能更正我的代码吗?或提出其他建议?

谢谢。

【问题讨论】:

标签: r dataframe ggplot2 bar-chart


【解决方案1】:

这里是示例数据,因为您没有提供太多可使用的数据:

df <- data.frame(Gene_Names = LETTERS, 
                 Cell_line1 = 10, 
                 Cell_line2 = 8)

以下是重塑数据的方式:

df <- df %>% tidyr::gather("Cell_line", "Value", 2:3) 

如您所描述的情节示例:

ggplot(data=df, aes(x = Gene_Names, y = Value, fill = Cell_line)) + 
  geom_bar(stat="identity", position = "dodge", width = .5)+ 
  theme(axis.text.x = element_text(angle = 90, hjust = 1))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-11
    • 1970-01-01
    • 1970-01-01
    • 2015-04-10
    • 2020-09-19
    • 1970-01-01
    • 2018-09-30
    • 1970-01-01
    相关资源
    最近更新 更多