【发布时间】:2023-03-06 22:11:01
【问题描述】:
更新
按照@danlooo 的建议,我设法让 vline 显示出来,
geom_vline(aes(xintercept= 4.7) # 4.7 is the rough position of the first value in VLineValues <- c(25, 26, 27), between level 12 & 40 (also position 4 and 5)
但是,由于我的真实数据中有很多图是在具有 3 条不同 vlines 的循环中生成的,因此很难找到一个公式来为所有 vlines 找到正确的位置。
因此,我尝试使用sec.axis = sec_axis() 添加辅助轴。
不幸的是,您不能将连续轴添加到离散上,您将收到以下错误
错误:提供给连续刻度的离散值
然后我尝试使用连续轴绘制数据并将分解后的数据添加为辅助轴。然而,这会将图形“挤压”在一起,使其松散成典型的 S 形。
所以我最终放弃了使用 ggplot 制作这些特定的绘图,而是使用了“普通”plot() 函数,尽管它看起来不如 ggplots 好,但效果很好。...
我正在尝试使用 geom_vline 向具有分解 x 轴(必须分解)的 ggplot 添加一条垂直线。但是这条线没有显示出来。
我尝试了以下几个建议的解决方案:
Positioning geom_vline with dates on x-axis
Draw geom_vline in discrete x axis
ggplot geom_vline on x-axis of class date
https://www.py4u.net/discuss/866042(此解决方案适用于python...)
https://github.com/tidyverse/ggplot2/issues/3197
https://github.com/tidyverse/ggplot2/issues/4285
https://community.rstudio.com/t/how-to-add-a-vertical-line-on-a-factor/38341
Issue with a drawing a vertical line in ggplot for categorical variable x-axis in R
这是我当前代码的示例:
library(drc)
library(ggplot2)
library(data.table)
library(tidyverse)
con <- c(0,1,4,12,40,100,300,1000, 0,1,4,12,40,100,300,1000,0,1,4,12,40,100,300,1000)
Vector <- c(1.2, 1.5, 1.8, 2, 2.7, 3, 3.5, 4,1.2, 1.5, 1.8, 2, 2.7, 3, 3.5, 4,1.2, 1.5, 1.8, 2, 2.7, 3, 3.5, 4)
df <- data.frame(Vector, con)
VLineValues <- c(25, 26, 27)
dfTemp <- df[1:8,]
dfTemp$X = con[1:8]
dfTemp$X <- factor(dfTemp$X, levels=unique(dfTemp$X))
print(ggplot(dfTemp, aes(x = X, y = Vector))+
ylim(1, 4.5)+
geom_point()+
geom_vline(aes(xintercept= VLineValues[1] )+
geom_smooth(method = drm, method.args = list(fct = LL.5()), se = FALSE))
我想要做的是遍历 df,得到 3 个单独的图,每个图在 x = 25、x=26 和 x=27 处各有一条线。
正如我上面提到的,我尝试了一些其他解决方案,您可以在其中获得关卡的位置,变异? tidyverse 等数据。
如果 x 轴是数字或例如我写,则 Vline 显示良好
geom_vline(1:5) #Gives Vlines at the 5 first levels in the factorised x-axis
但是,它不会显示在分解后的 x 轴上的水平之间
关于如何解决这个问题的任何建议?
【问题讨论】:
标签: r loops ggplot2 x-axis vline