R 将两个图形转换为一个

R 将两个图形转换为一个,r,dataframe,ggplot2,plot,R,Dataframe,Ggplot2,Plot,我面临一些问题,从单独的数据帧中获得一个绘图,而不是两个绘图。我在下面解释了一下情况。数据帧看起来像: df1 <- structure(list(value = c(9921L, 21583L, 11822L, 1054L, 13832L, 16238L, 13838L, 20801L, 20204L, 13881L, 19935L, 13829L, 14012L, 20654L, 13862L, 21191L, 3777L, 15552L, 13817L, 20428L, 1685

我面临一些问题,从单独的数据帧中获得一个绘图,而不是两个绘图。我在下面解释了一下情况。数据帧看起来像:

df1 <- structure(list(value = c(9921L, 21583L, 11822L, 1054L, 13832L, 
16238L, 13838L, 20801L, 20204L, 13881L, 19935L, 13829L, 14012L, 
20654L, 13862L, 21191L, 3777L, 15552L, 13817L, 20428L, 16850L, 
21003L, 11072L, 22477L, 12321L, 12856L, 16295L, 11431L, 13469L, 
14680L, 10552L, 15272L, 9132L, 9374L, 15123L, 22754L, 10363L, 
12160L, 13729L, 11151L, 11451L, 11272L, 14900L, 14688L, 17133L, 
7315L, 7268L, 6262L, 72769L, 7650L, 16389L, 13027L, 7134L, 6465L, 
6490L, 15183L, 7201L, 14070L, 11210L, 10146L), limit = structure(c(1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("1Mbit", 
"5Mbit", "10Mbit"), class = "factor")), class = "data.frame", row.names = c(NA, 
-60L))

df2 <- structure(list(value = c(37262L, 39881L, 30914L, 32976L, 28657L, 
39364L, 39915L, 30115L, 29326L, 36199L, 37976L, 36694L, 33718L, 
36945L, 33182L, 35866L, 34188L, 33426L, 32804L, 34986L, 29355L, 
30470L, 37420L, 26465L, 28975L, 29144L, 27491L, 30507L, 27146L, 
26257L, 31231L, 30521L, 30370L, 31683L, 33774L, 35654L, 34172L, 
38554L, 38030L, 33439L, 34817L, 31278L, 33579L, 31175L, 31001L, 
29908L, 31658L, 33381L, 28709L, 34794L, 34154L, 30157L, 33362L, 
30363L, 31097L, 29116L, 27703L, 31229L, 30196L, 30077L), limit = structure(c(3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("180ms", 
"190ms", "200ms"), class = "factor")), class = "data.frame", row.names = c(NA, 
-60L))
我正在寻求您的帮助,将它们合并到一个图中(将
bw_线
lt_线
放在同一个图形中),在顶部和底部显示两个x轴,或在底部显示两个轴的标题。请注意,
值对于每个数据集都有不同的范围。但是,我需要为每个数据帧的不同范围显示两个y轴,或者一个y轴显示两个数据帧的所有值(最小值到最大值)

实际上,我在@RichieCotton中看到了一个非常接近的解决方案,但无法计算出我的数据,因为我有一些
因子
,而不是整数值


我真的很感谢你的帮助。谢谢。

我认为首先将数据合并到一个数据帧中可能是最简单的方法。在这里,我创建组合x值并将数据映射到这些值。然后我们像往常一样映射,添加一个辅助y轴

library(tidyverse); library(forcats)

# Create shared x axis and combine data frames
limit_combo <- data.frame(level_num = 1:3, 
                          level = as_factor(c("1Mbit\n200ms",
                                              "5Mbit\n190ms",
                                              "10Mbit\n180ms"))) 
df1b <- df1 %>%
  mutate(level_num = limit %>% as.numeric) %>%
  left_join(limit_combo)
df2b <- df2 %>%
  mutate(level_num = 4 - (limit %>% as.numeric)) %>%
  left_join(limit_combo)
df3 <- bind_rows(df1b, df2b, .id = "plot") %>%
  mutate(plot = if_else(plot == "1", "bw", "lt"))

# plot with adjusted y values and second axis for reference
ggplot(df3, aes(x = level, 
                y = value * if_else(plot == "lt", 0.44, 1), 
                group=plot, color = plot)) + 
  geom_quantile(method = "loess") +
  scale_y_continuous("value", sec.axis = sec_axis(~./0.44)) +
  theme(axis.text.y.left  = element_text(color = "#F8766D"),
        axis.text.y.right = element_text(color = "#00BFC4"))
library(tidyverse);图书馆(供猫用)
#创建共享x轴并合并数据帧
限制\u组合%as.numeric)%>%
左联合(限制联合)
df2b%
变异(级别_num=4-(限制%>%as.numeric))%%>%
左联合(限制联合)
df3%
变异(绘图=如果其他(绘图=“1”、“bw”、“lt”))
#用调整后的y值和第二个轴绘制以供参考
ggplot(df3,aes(x=水平,
y=值*如果(绘图=“lt”,0.44,1),
组=绘图,颜色=绘图))+
geom_分位数(方法=“黄土”)+
比例y连续(“值”,秒轴=秒轴(~./0.44))+
主题(axis.text.y.left=element_text(color=“#F8766D”),
axis.text.y.right=元素_文本(color=“#00BFC4”))

这里有一种不同的方法,可以从两个数据集创建一个绘图,避免将两个数据集合并为一个,并处理
限制的因素<代码>df1
df2
限制_bw
限制_lt
按照OP的规定使用

情节分三步细化

1.公共x轴,公共y刻度

2.单独的x轴,通用的y刻度

3.分离x轴,分离y轴 这里,对第二个数据集的y值进行缩放,以使两个数据集的最小值和最大值重合

# compute scaling factor and offset
library(magrittr)   # used to improve readability
bw_rng <- loess(df1$value ~ as.integer(limit_bw)) %>% fitted() %>% range()
lt_rng <- loess(df2$value ~ as.integer(limit_lt)) %>% fitted() %>% range()
scl <- diff(bw_rng) / diff(lt_rng)
ofs <- bw_rng[1] - scl * lt_rng[1]
library(ggplot2)
ggplot() +
  geom_quantile(aes(x = as.integer(limit_bw), y = value, colour = "bw"), 
                df1, method = "loess") + 
  geom_quantile(aes(x = as.integer(limit_lt), y = scl * value + ofs, colour = "lt"), 
                df2, method = "loess") +
  scale_x_continuous("limit",
                     breaks = 1:nlevels(limit_bw), 
                     labels = levels(limit_bw), 
                     sec.axis = dup_axis(labels = levels(limit_lt))) +
  scale_y_continuous(sec.axis = sec_axis(~ (. - ofs) / scl)) +
  scale_colour_manual(NULL, values = c(bw = "blue", lt = "red")) +
  theme(axis.text.x.bottom = element_text(color = "blue"),
        axis.text.x.top    = element_text(color = "red"),
        axis.text.y.left   = element_text(color = "blue"),
        axis.text.y.right  = element_text(color = "red"))
#计算比例因子和偏移量
图书馆(magrittr)#用于提高可读性
bw_rng%已安装()%%>%范围()
lt_rng%已安装()%>%范围()

scl你能在Paint/Word/PPT中画出预期的输出,然后发布到这里吗?@Tung,我想要一个类似的图,如Jon Spring的回复所示。这非常有用,谢谢你的时间@JonSpring@samm如果这个或任何答案已经解决了你的问题,请考虑点击复选标记。这将帮助将来可能遇到同样问题的读者。这也会给回答者和你自己带来一些声誉。没有义务这样做。
grid.draw(rbind(ggplotGrob(ggplot(df1, aes(limit_bw,value,group=1)) + geom_quantile(method = "loess") + labs(title = "value vs bw",x="bandwidth",y="value")),
ggplotGrob(ggplot(df2, aes(limit_lt, value, group = 1)) + geom_quantile(method="loess") + labs(title="value vs latency", x="latency", y="value")), size = "last"))
library(tidyverse); library(forcats)

# Create shared x axis and combine data frames
limit_combo <- data.frame(level_num = 1:3, 
                          level = as_factor(c("1Mbit\n200ms",
                                              "5Mbit\n190ms",
                                              "10Mbit\n180ms"))) 
df1b <- df1 %>%
  mutate(level_num = limit %>% as.numeric) %>%
  left_join(limit_combo)
df2b <- df2 %>%
  mutate(level_num = 4 - (limit %>% as.numeric)) %>%
  left_join(limit_combo)
df3 <- bind_rows(df1b, df2b, .id = "plot") %>%
  mutate(plot = if_else(plot == "1", "bw", "lt"))

# plot with adjusted y values and second axis for reference
ggplot(df3, aes(x = level, 
                y = value * if_else(plot == "lt", 0.44, 1), 
                group=plot, color = plot)) + 
  geom_quantile(method = "loess") +
  scale_y_continuous("value", sec.axis = sec_axis(~./0.44)) +
  theme(axis.text.y.left  = element_text(color = "#F8766D"),
        axis.text.y.right = element_text(color = "#00BFC4"))
library(ggplot2)
ggplot() + aes(y = value) +
  geom_quantile(aes(x = as.integer(limit_bw), colour = "bw"), df1, method = "loess") + 
  geom_quantile(aes(x = as.integer(limit_lt), colour = "lt"), df2, method = "loess") +
  scale_x_continuous("limit",
    breaks = 1:nlevels(limit_bw), 
    labels = paste(levels(limit_bw), levels(limit_lt), sep = "\n")) +
  scale_colour_discrete(NULL)
library(ggplot2)
ggplot() + aes(y = value) +
  geom_quantile(aes(x = as.integer(limit_bw), colour = "bw"), df1, method = "loess") + 
  geom_quantile(aes(x = as.integer(limit_lt), colour = "lt"), df2, method = "loess") +
  scale_x_continuous("limit",
                     breaks = 1:nlevels(limit_bw), 
                     labels = levels(limit_bw), 
                     sec.axis = dup_axis(labels = levels(limit_lt))) +
  scale_colour_manual(NULL, values = c(bw = "blue", lt = "red")) +
  theme(axis.text.x.bottom = element_text(color = "blue"),
        axis.text.x.top    = element_text(color = "red"))
# compute scaling factor and offset
library(magrittr)   # used to improve readability
bw_rng <- loess(df1$value ~ as.integer(limit_bw)) %>% fitted() %>% range()
lt_rng <- loess(df2$value ~ as.integer(limit_lt)) %>% fitted() %>% range()
scl <- diff(bw_rng) / diff(lt_rng)
ofs <- bw_rng[1] - scl * lt_rng[1]
library(ggplot2)
ggplot() +
  geom_quantile(aes(x = as.integer(limit_bw), y = value, colour = "bw"), 
                df1, method = "loess") + 
  geom_quantile(aes(x = as.integer(limit_lt), y = scl * value + ofs, colour = "lt"), 
                df2, method = "loess") +
  scale_x_continuous("limit",
                     breaks = 1:nlevels(limit_bw), 
                     labels = levels(limit_bw), 
                     sec.axis = dup_axis(labels = levels(limit_lt))) +
  scale_y_continuous(sec.axis = sec_axis(~ (. - ofs) / scl)) +
  scale_colour_manual(NULL, values = c(bw = "blue", lt = "red")) +
  theme(axis.text.x.bottom = element_text(color = "blue"),
        axis.text.x.top    = element_text(color = "red"),
        axis.text.y.left   = element_text(color = "blue"),
        axis.text.y.right  = element_text(color = "red"))