【问题标题】:Graphic with ggplot2, one categorical variable and four numeric [duplicate]带有ggplot2的图形,一个分类变量和四个数字[重复]
【发布时间】:2018-02-16 20:20:44
【问题描述】:

我正在尝试从 R 中的 excel 复制图形,但使用 ggplot2。但是我还没有找到一种方法来做到这一点,即使使用融化或其他功能,我也无法找到一种方法来表示我的所有数据。顺便说一句,你能帮我用我需要用来从excel中获取它并直接做图形的代码吗,因为当我试图用read.table复制第一列时,它没有像我一样被当作X轴喜欢,所以我只是尝试将所有列复制为独立变量,然后将其设为数据框。但我会花很多时间,因为我有 30 张桌子。请让我现在怎么做。

这是我的数据和我想复制的图形:

pro = c("CONCOCT", "MetaBAT", "MaxBin", "MyCC")
MEGAHIT = c(18, 6, 5, 7)
RayMeta = c(5, 3, 2, 4)
SPAdes = c(23, 4, 5, 5)
Velvet = c(20, 2, 4, 7)

如果您能提供一些帮助,我将不胜感激。

【问题讨论】:

  • 寻求帮助时,您应该包含一个简单的reproducible example,其中包含可用于测试和验证可能解决方案的示例输入和所需输出。发布数据图片没有帮助——我们不喜欢花时间重新输入它们。
  • @MrFlick 抱歉,我已经编辑了问题,谢谢您的评论

标签: r ggplot2 graphics


【解决方案1】:

不要绝望。一步步。很好,你开始使用 R :) 你需要一些基本的阅读,这是肯定的。但这里有一些建议。首先,使您的示例可重现,以便我们只需要复制粘贴它。意思是用赋值运算符完成。 将数据导入 R 的一个好方法是将您的 excel 保存为 .csv,然后使用 utils 包中的read.csv() 导入此 csv

# a way to make your example more reproducible,
df1 <- data.frame (pro = c("CONCOCT", "MetaBAT", "MaxBin", "MyCC"),
MEGAHIT = c(18, 6, 5, 7),
RayMeta = c(5, 3, 2, 4),
SPAdes = c(23, 4, 5, 5),
Velvet = c(20, 2, 4, 7), stringsAsFactors = F)

# To your questions 
# install required packages 
require(ggplot2) #must have package for plotting
require(tidyr) # must have package for tidy data
require(dplyr) # must have package for aggregating and summarising

#1 as noted in the answer which @MrFlick was pointing to, 
#ggplot needs long data, therefore gather your dataframe
df1 <- df1 %>% gather(key, value, 2:5)

#2 make a nice plot 
ggplot(df1, aes(x = key, y = value, group = pro, colour = pro)) + geom_line()

最后但同样重要的是……ggplot 食谱是你的朋友

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-11-29
    • 2018-01-07
    • 1970-01-01
    • 2021-07-26
    • 2017-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多