如果x轴中有geom_vline
,我在添加factor
时遇到问题。我想转换为x_value
到factor
并绘制一条带x_line
列的垂直线。
类似的帖子可能是这个。
geom_vline vertical line on x-axis with categorical data: ggplot2
但是即使我按照下面所示的方式发布了
,它也没有用以下是问题的简短版本
df <- data.frame(x_value=c(rep(seq(1,5),2)), y_value=c(rnorm(10,5,1)),x_vline=3, gr=rep(c(1,2),c(5,5)))
> df
x_value y_value x_vline gr
1 1 6.589680 3 1
2 2 4.937223 3 1
3 3 6.934755 3 1
4 4 4.407492 3 1
5 5 6.321616 3 1
6 1 7.068435 3 2
7 2 4.379096 3 2
8 3 8.138772 3 2
9 4 5.814828 3 2
10 5 5.828453 3 2
library(ggplot2)
ggplot(df, aes(x=factor(x_value), y=y_value)) +
geom_point(size=3, aes(colour=factor(gr)))+
facet_wrap(~gr)+
geom_vline(aes(xintercept=factor(x_vline)), linetype="dashed",colour="blue",size=0.7)
UseMethod(“rescale”)中的错误: 没有适用于“rescale”的方法适用于“factor”类的对象
然后按照此Issue with a drawing a vertical line in ggplot for categorical variable x-axis in R和此geom_vline vertical line on x-axis with categorical data: ggplot2
我试过
ggplot(df, aes(x=factor(x_value), y=y_value)) +
geom_point(size=3, aes(colour=factor(gr)))+
facet_wrap(~gr)+
#geom_vline(aes(xintercept=factor(x_vline)), linetype="dashed",colour="blue",size=0.7)
geom_vline(df,aes(xintercept=which(levels(factor(x_value)) %in% levels(factor(x_vline)))), linetype="dashed",colour="blue",size=0.7)
错误:ggplot2不知道如何处理class uneval的数据
我该如何使这项工作? 谢谢!