R 将基于数据的点添加到ggplot
我有一个问题,我必须根据数据中的值向现有绘图添加一个“目标”点 例如,在reprex中——指出(2010605)。(目标年,占2008年利润的110%) 我知道我可以在绘图之前计算。。。但是有没有一种方法可以使用.data代词在ggplot中获得2008年的利润 雷普雷克斯:R 将基于数据的点添加到ggplot,r,ggplot2,tidyeval,R,Ggplot2,Tidyeval,我有一个问题,我必须根据数据中的值向现有绘图添加一个“目标”点 例如,在reprex中——指出(2010605)。(目标年,占2008年利润的110%) 我知道我可以在绘图之前计算。。。但是有没有一种方法可以使用.data代词在ggplot中获得2008年的利润 雷普雷克斯: library(ggplot2) sales <- data.frame( year = c(2005, 2006, 2007, 2008), profit = c(340, 500, 600, 550)
library(ggplot2)
sales <- data.frame(
year = c(2005, 2006, 2007, 2008),
profit = c(340, 500, 600, 550)
)
sales %>%
ggplot() +
aes(x = year, y = profit) +
geom_line() +
# throws error: Error: Discrete value supplied to continuous scale
geom_point(aes(x = 2010, y = .data[["year"]] == 2008))
# calculate before plot
pull(sales[sales[["year"]] == 2008, ]["profit"])
库(ggplot2)
销售额%
ggplot()+
aes(x=年度,y=利润)+
geom_线()+
#抛出错误:错误:提供给连续刻度的离散值
geom_点(aes(x=2010年,y=2008年)
#绘图前计算
拉动(销售额[销售额[[“年”]]==2008,][“利润”])
您可以使用:
library(ggplot2)
ggplot(sales) + aes(x = year, y = profit) +
geom_line() +
geom_point(aes(x = 2010, y = .data[['profit']][.data[["year"]] == 2008]))
您可以使用:
library(ggplot2)
ggplot(sales) + aes(x = year, y = profit) +
geom_line() +
geom_point(aes(x = 2010, y = .data[['profit']][.data[["year"]] == 2008]))