R 组合不同长度数据帧的GGPLOT

R 组合不同长度数据帧的GGPLOT,r,ggplot2,plot,histogram,density-plot,R,Ggplot2,Plot,Histogram,Density Plot,我有两个不同长度的数据帧的GGPlot,我分别绘制了一列的直方图,如下所示。我想把这两个图合并成一个带有两个不同y轴的ggplot,一个在右边,一个在左边,用于两个数据帧。我该怎么做 a = ggplot(GG, aes(x = as.numeric(Kstat))) + theme_pubclean() a + geom_density() + geom_vline(aes(xintercept = mean(Kstat)), linetype = &qu

我有两个不同长度的数据帧的GGPlot,我分别绘制了一列的直方图,如下所示。我想把这两个图合并成一个带有两个不同y轴的ggplot,一个在右边,一个在左边,用于两个数据帧。我该怎么做

a = ggplot(GG, aes(x = as.numeric(Kstat))) +
  theme_pubclean()

a + geom_density() +
  geom_vline(aes(xintercept = mean(Kstat)), 
             linetype = "dashed", size = 0.6) + xlim(0,1000)+ylim(0,0.1)

b = ggplot(all, aes(x = as.numeric(Kstat))) +
  theme_pubclean()

b + geom_density() +
  geom_vline(aes(xintercept = mean(Kstat)), 
             linetype = "dashed", size = 0.6) + xlim(0,1000)+ylim(0,0.5)

我们没有您的数据,因此这里有一个示例,其中包含
ggplot2
中的数据集:

library(ggplot2)
df1 <- diamonds[1:10,7]
df2 <- diamonds[100:2100,7]

解决这个问题的一种方法是将df2密度放大25倍,并使用反向调整创建一个次轴。(这就是在ggplot2中次轴的工作方式;首先将数据缩放到主轴,然后创建次轴作为注释,以帮助读者解释它。)


没有时间做一个例子。退房请注意,虽然这是可行的,但请考虑是否将两个图形相邻放置以更好的方式传达您的信息。将数据转换为长格式,然后将要比较的图分面。如果您可以为两个数据帧添加一些示例数据,这将有助于测试和验证解决方案。使用
dput(您的数据框)将您的数据粘贴到问题中。
提供了一些关于提出一个好问题的提示。“我只想知道我的数据有多少落在一系列数字中,比如直方图。”这是一个模糊的问题,如果没有更多解释,将很难回答。另外,我无法从你的汇总数据列表中理解——我们应该从中看到什么?对不起。请不要理会我刚才说的话。我被误解了。
ggplot() +
  geom_density(data = df1, aes(x = price)) +
  geom_vline(data = df1, aes(xintercept = mean(price)), 
             linetype = "dashed", size = 0.6) +
  geom_density(data = df2, aes(x = price), color = "red") +
  geom_vline(data = df2, aes(xintercept = mean(price)), 
             linetype = "dashed", color = "red", size = 0.6) 
ggplot() +
  geom_density(data = df1, aes(x = price)) +
  geom_vline(data = df1, aes(xintercept = mean(price)), 
             linetype = "dashed", size = 0.6) +
  geom_density(data = df2, aes(x = price, y = ..density.. * 25), color = "red") +
  geom_vline(data = df2, aes(xintercept = mean(price)), 
             linetype = "dashed", color = "red", size = 0.6) +
  scale_y_continuous(sec.axis = ~ . / 25) +
  theme(axis.text.y.right = element_text(color = "red"))