问题描述
我想在一个ggplot 上标注一个水平线,并带有多个系列,而不需要将该线与一个系列相关联。
我如何标记行没有将标签与系列标签关联?
这是您的想法吗? b
<$ p $ (数据帧)(y = 1:10,x = 1:10,col = c(a,b))#添加了col
h ggplot(df,aes(x = x,y = y))+
geom_point(aes(color = col))+
geom_hline yintercept = h)+
geom_text(data = data.frame(x = 0,y = h),aes(x,y),label = h,vjust = -1)
首先,您可以将颜色映射局部应用于点图层。其次,您不必将所有美学内容都放入对 aes(...)
的调用中 - 只有那些您希望映射到数据集列中的美学。三,你可以在 data = ...
中调用特定的geom _ *。
来创建特定于图层的数据集
I'd like to label a horizontal line on a ggplot with multiple series, without associating the line with a series. R ggplot2: Labelling a horizontal line on the y axis with a numeric value asks about the single-series case, for which geom_text
solves. However, geom_text
associates the label with one of the series via color and legend.
Consider the same example from that question, with another color
column:
library(ggplot2)
df <- data.frame(y=1:10, x=1:10, col=c("a", "b")) # Added col
h <- 7.1
plot1 <- ggplot(df, aes(x=x, y=y, color=col)) + geom_point()
plot2 <- plot1 + geom_hline(aes(yintercept=h))
# Applying top answer https://stackoverflow.com/a/12876602/1840471
plot2 + geom_text(aes(0, h, label=h, vjust=-1))
How can I label the line without associating the label to one of the series?
Is this what you had in mind?
library(ggplot2)
df <- data.frame(y=1:10, x=1:10, col=c("a", "b")) # Added col
h <- 7.1
ggplot(df, aes(x=x,y=y)) +
geom_point(aes(color=col)) +
geom_hline(yintercept=h) +
geom_text(data=data.frame(x=0,y=h), aes(x, y), label=h, vjust=-1)
First, you can make the color mapping local to the points layer. Second, you do not have to put all the aesthetics into calls to aes(...)
- only those you want mapped to columns of the dataset. Three, you can have layer-specific datasets using data=...
in the calls to a specific geom_*.
这篇关于R ggplot2:标记水平线而不将标签与系列相关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!