【问题标题】:Combine two scatter plots with different data points将两个散点图与不同的数据点结合起来
【发布时间】:2018-04-01 06:13:18
【问题描述】:

我有一个要求,我想在同一个绘图区域中组合两个不同的散点图。一个散点图带有度量 1,另一个散点图带有度量 2。 在 R 中可行吗? 我已经添加了数据集和代码。但不确定如何将这两者合并到同一个情节中。

df1 <- data.frame(Product = c("A","B","C"),
                  ProductMetric =  c("85","90","92"),
                  CategoryMetric = c("83"),
                  Category = c("AAA"))
df1
ggplot(data=df1, mapping= aes(x=Category,y= ProductMetric))+ geom_point(size=5)+
  ggplot(data=df1, mapping= aes(x=Category,y= CategoryMetric))+ geom_point(size=5)

所以基本上在组合结果之后,同一个图表中应该有 4 个圆圈,基本上我想在同一个图表中用圆圈显示 Product Avg 和 Category Avg,以便最终用户可以将产品 avg 与类别 avg 进行比较只看图表。

问候, 阿卡什

【问题讨论】:

  • 感谢您的快速回复。感谢您的帮助。

标签: r ggplot2


【解决方案1】:

您只需使用tidyr 包中的gather 将数据从wide 转换为long 格式。阅读更多here

library(dplyr)  
library(tidyr)
library(ggplot2)

df1 <- data.frame(Product = c("A","B","C"),
                  ProductMetric =  c("85","90","92"),
                  CategoryMetric = c("83"),
                  Category = c("AAA"))
df1

#>   Product ProductMetric CategoryMetric Category
#> 1       A            85             83      AAA
#> 2       B            90             83      AAA
#> 3       C            92             83      AAA

df1_long <- df1 %>% 
  gather(key, value, -Category, -Product)
df1_long

#>   Product Category            key value
#> 1       A      AAA  ProductMetric    85
#> 2       B      AAA  ProductMetric    90
#> 3       C      AAA  ProductMetric    92
#> 4       A      AAA CategoryMetric    83
#> 5       B      AAA CategoryMetric    83
#> 6       C      AAA CategoryMetric    83

ggplot(df1_long, aes(x = Category, y = value, color = key)) + geom_point(size = 5)

编辑:将Category Ave 的颜色保留在red 中,同时根据产品数量动态更改每个Product 的颜色和图例。

myCol <- c(RColorBrewer::brewer.pal(length(unique(df1$Product)), "Set2"), "red")

ggplot(df1, aes(x = Product, y = ProductMetric, color = Product)) + geom_point(size = 5) +
  geom_point(data = df1, aes(y = CategoryMetric, color = "Category Ave"), size = 5) +
  scale_color_manual("Legend", 
                     labels = c(paste0("Product ", df1$Product), "Category Ave"),
                     values = myCol)

ggplot(df1, aes(x = Category, y = ProductMetric, color = Product)) + geom_point(size = 5) +
  geom_point(data = df1, aes(y = CategoryMetric, color = "Category Ave"), size = 5) +
  scale_color_manual("Legend", 
                     labels = c(paste0("Product ", df1$Product), "Category Ave"),
                     values = myCol)

reprex package (v0.2.0) 于 2018 年 3 月 31 日创建。

【讨论】:

  • 我想动态更改每个产品的颜色。并将 Category avg 保持为“Red”
  • 我的意思是 3 种产品的三种不同颜色.. :)
【解决方案2】:

我们可以添加一个新的geom_point 层并指定datadf1yCategoryMetric

library(ggplot2)

ggplot(data = df1, mapping = aes(x = Category, y = ProductMetric)) + 
  geom_point(size = 5) +
  geom_point(data = df1, mapping = aes(x = Category, y = CategoryMetric), size = 5, color = "red")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-03
    • 2021-09-04
    • 1970-01-01
    • 1970-01-01
    • 2022-10-18
    • 2021-08-21
    • 2018-03-03
    相关资源
    最近更新 更多