【问题标题】:Plotting multiple grouped variable datasets in ggplot在 ggplot 中绘制多个分组的变量数据集
【发布时间】:2018-07-02 17:42:07
【问题描述】:

我正在尝试在 ggplot 中绘制具有分组变量的多个数据集,但遇到了一些问题。好的,所以我有两个数据集:

df.1 <- data.frame(
name = c( "a", "b", "c", "d" ),
x = c( 3, 2, 1, 2 ),
y = c( 4, 3, 4, 3 ),
z = c( 8, 9, 6, 7 ) )

df.2 <- data.frame(
name = c( "o", "p", "q", "r" ),
x = c( 8, 7, 6, 9 ),
y = c( 4, 1, 4, 3 ),
z = c( 1, 2, 2, 2 ) )

然后我将它们中的每一个按名称分组

df.1.melted  <- melt( df.1, id.vars = "name" )
df.2.melted  <- melt( df.2, id.vars = "name" )

现在,我想要一个图,其中 x 轴有 xyz 分组,y 轴是值,每个样本都由 name 链接给它.我可以为其中一个数据集执行此操作(我最终想要一个对数刻度,因此包含在内):

ggplot( df.1.melted, aes( x = variable, 
                          y = value, 
                          group = df.1.melted$name, 
                          col = df.1.melted$name ) ) +
scale_y_continuous( trans = log_trans(), limits = c( 1, 10 ), 
                    breaks = c( 1, 10 ) ) +
labs( x = "", y = "value" ) +
geom_point( size = 4 ) +
geom_line( size = 1 ) 

这给了我一些合理的东西:

然后我可以通过以下方式添加第二个数据集:

ggplot( df.1.melted, aes( x = variable, 
                          y = value, 
                          group = df.1.melted$name, 
                          col = df.1.melted$name ) ) +
scale_y_continuous( trans = log_trans(), limits = c( 1, 10 ), 
                    breaks = c( 1, 10 ) ) +
labs( x = "", y = "value" ) +
geom_point( size = 4 ) +
geom_line( size = 1 ) +

geom_point( data = df.2.melted, aes( x = df.2.melted$variable,
                                     y = df.2.melted$value, 
                                     group = df.2.melted$name, 
                                     col = df.2.melted$name ), 
            size = 4 ) +
geom_line( data = df.2.melted, aes( x = df.2.melted$variable,
                                    y = df.2.melted$value, 
                                    group = df.2.melted$name, 
                                    col = df.2.melted$name ), 
           size = 1 ) 

产生:

这是我所追求的主题,但我遇到了一些问题: 1) 使用aes( group = ...) 部分时如何覆盖默认配色方案?我想在数据框中有预定义的颜色,或者能够在geom_point() 中定义它们。颜色应该特定于我正在使用的数据框,所以df.1.melteddarkgreendf.2.meltedorange 或类似的东西。我还没有找到如何在不使用group = 调用aes() 的情况下绘制这些图,所以我目前找不到解决方法。

该解决方案看起来可行,如此处答案中的ggplot 示例: R plotly - Plotting grouped lines

但是,我对dplyr 不够熟悉,无法弄清楚创建这个情节的原因。

感谢您的建议

【问题讨论】:

  • scale_colour_manual 让您手动分配颜色

标签: r ggplot2


【解决方案1】:

你可以试试这个

library(ggplot2)
library(dplyr)
df_melted <- bind_rows(df.1.melted, df.2.melted)
df_melted %>% 
 mutate(df = rep(c('df.1', 'df.2'), each = nrow(df_melted) / 2)) %>% 
 ggplot(aes(x = variable,
            y = value,
            col = df)) +
 geom_line(aes(group = name)) +
 geom_point() +
 scale_y_log10(limits = c( 1, 10), 
               breaks = c(1, 10)) +
 scale_color_manual(values = c('df.1' = "forestgreen",
                               'df.2' = "orange"))

这个想法是创建一个数据框df_melted,并添加列df,指示观察来自哪个数据框。然后您可以将变量df 映射到颜色美学。正如评论中所建议的,您可以使用 scale_colour_manual 更改默认颜色。

【讨论】:

  • 嗯,好的。所以这正是我正在寻找的。你能告诉我mutate() 函数在做什么吗?抱歉,我对此还是有点陌生​​,我不了解第一次管道通过后会发生什么。
  • mutate 添加新变量。你也可以这样做df$new_variable &lt;- "a string"。有关详细信息,请参阅 ?mutate 或在 R for Data Science 中阅读有关此功能的信息
  • @Jesse,对您的情节再发表评论。建议不要aes() 中引用带有$ 的变量。这可能会导致问题。仅使用变量名,即使用var 而不是df$var
  • 嗨@mar​​kus,感谢您的绘图技巧,这完全有道理。我仍然对管道的使用感到困惑,无法弄清楚最终数据帧的样子。里面有什么正在绘制的?对不起,我在这里的无知..
  • 没有发生太多事情。在调用mutate 等之前先查看df_melted。下一步只需将以下向量添加到它rep(c('df.1', 'df.2'), each = nrow(df_melted) / 2) 并调用它df
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-23
  • 2018-08-30
  • 2016-10-07
  • 2020-12-06
  • 2020-11-25
  • 2018-10-07
相关资源
最近更新 更多