结合两个散点图和不同的数据点 [英] Combine two scatter plots with different data points

查看:59
本文介绍了结合两个散点图和不同的数据点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要在同一绘图区中合并两个不同散点图的要求.一个散点图是度量1,另一个散点图是度量2. 在R中可行吗? 我添加了数据集以及代码.但不确定如何将这两个图合并到同一图中.

I have a requirement where I want to combine two different scatter plot in the same plot area. One scatter plot is with metric 1 and another scatter plot with metric 2. Is it feasible in R ? I have added the dataset as well as code. But not sure how to merge these two within the same plot.

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个圆圈,基本上我想在同一张图表中显示带有该圆圈的产品平均和类别平均,以便最终用户可以将产品平均与类别平均进行比较只需查看图表即可.

So basically after the combined result, there should be 4 circles in the same chart, Basically I want to show Product Avg and Category Avg with the circle in the same chart so that end user can compare the product avg with the category avg by just seeing the chart.

关于, 阿卡什(Akash)

Regards, Akash

推荐答案

您只需要使用tidyr包中的gather将数据从wide转换为long格式.在此处

You only need to convert your data from wide to long format using gather from the tidyr package. Read more 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的颜色和图例.

to keep Category Ave color in red while changing the color and legend for each Product dynamically depending on the number of products.

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程序包(v0.2.0)创建于2018-03-31.

Created on 2018-03-31 by the reprex package (v0.2.0).

这篇关于结合两个散点图和不同的数据点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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