2

我正在尝试在 ggplot 中生成哑铃图,同时还尝试记录转换 x 和 y 轴。系统抛出我无法理解的错误。寻求帮助。

这是示例代码:

#create the data
df <- data.frame(x = c(1, 2, 3, 100),
                 xend = c(2, 4, 6, 110),
                 y = c(1, 2, 3, 100)) 

#plot the untransformed dumbbell plot
library('ggalt')
ggplot(df, aes(x = x, xend = xend, y = y)) + 
  geom_dumbbell()

在此处输入图像描述

#Try the plot with coordinate transformation
ggplot(df, aes(x = x, xend = xend, y = y)) + 
  geom_dumbbell() + 
  coord_trans(x = 'log2', y = 'log2')

抛出错误:

[.data.frame(df, , c("alpha", "color", "size", "linetype"))中的错误:
选择了未定义的列

如果我coord_trans这样修改coord_trans(x = 'log2', xend = 'log2', y = 'log2),我会收到错误消息:

coord_trans(x = "log2", xend = "log2", y = "log2") 中的错误:
未使用的参数 (xend = "log2")

我想要的是相当于这个,geom_dumbbell()而不是geom_point()

ggplot(df, aes(x = x, y = y)) + 
  geom_point() + 
  coord_trans(x = 'log2', y = 'log2')

在此处输入图像描述

关于如何让 geom_dumbbell() 与 coord_trans() 一起工作的任何想法?

4

1 回答 1

1

这很奇怪,我不知道为什么它不适用于geom_dumbbell. 我也不知道它的geom_dumbbell存在!所以这是我多年来一直在使用geom_linerangegeom_point构建组件的 hack:

ggplot(df, aes(x=x, y = y)) + 
  geom_linerange(aes(xmin=x, xmax=xend)) + 
  geom_point() + 
  geom_point(aes(x=xend)) +
  coord_trans(x = 'log2', y = 'log2')
于 2021-01-07T17:20:41.883 回答