R 如何在ggplot2中混合aes_u2;()和算术计算?
我试图在R 如何在ggplot2中混合aes_u2;()和算术计算?,r,ggplot2,nse,aesthetics,R,Ggplot2,Nse,Aesthetics,我试图在R中的ggplot2包中的以下脚本中调整x和ymax,以调整errorbar的打印坐标,但它返回错误 gplot <- function(prd) { ggplot() + geom_polygon(data=shp.t,aes(x=long,y=lat,group=group), fill="white",colour="grey") + ## Plot errorbar
R
中的ggplot2
包中的以下脚本中调整x
和ymax
,以调整errorbar
的打印坐标,但它返回错误
gplot <- function(prd) {
ggplot() +
geom_polygon(data=shp.t,aes(x=long,y=lat,group=group),
fill="white",colour="grey") +
## Plot errorbar
geom_errorbar(data=te10.cent,size=2,colour="red",
alpha=.8,width=0,
aes_(x=quote(long.cent)-350,ymin=quote(lat.cent),
ymax=quote(lat.cent)+prd))
}
gplot("Field Name") # Not number but field name of the data frame
如果脚本中省略了-350
和+prd
,或者在数据帧中使用带有实际变量的“aes”,则在这两种情况下都有效。
我试过其他剧本<代码>“long.cent”-350和“lat.cent”+prd
而不是上面的脚本,但是它也返回相同的错误
我寻找解决方案,但他们都解释了如何在不混合参数和算术计算的情况下使用aes。我需要混合非标准表达式和算术计算来调整我的绘图,但如何调整?我相信这将解决您的问题:
gplot <- function(prd) {
ggplot() +
geom_polygon(data = shp.t,
aes_(x = ~long,
y = ~lat,
group = ~group),
fill = "white",
colour = "grey") +
## Plot errorbar
geom_errorbar(data = te10.cent,
size = 2,
colour = "red",
alpha = .8,
width = 0,
aes_(x = ~long.cent - 350,
ymin = ~lat.cent,
ymax = ~lat.cent + prd))
}
随着即将发布的ggplot2 2.3.0(将于2018年6月下旬发布),
aes_uu(…)
被软性弃用,建议使用tidy eval。这意味着在这里的上下文中,您要编写!!prd
当您需要变量prd
的内容而不是符号prd
时
库(ggplot2)
图书馆(dplyr)
#>
#>正在附加包:“dplyr”
#>以下对象已从“package:stats”屏蔽:
#>
#>滤波器,滞后
#>以下对象已从“package:base”屏蔽:
#>
#>相交、setdiff、setequal、并集
虹膜%>%
组别(种类)%>%
总结所有(~mean()->总结
gplot您是否尝试过使用scale\u x\u continuous
/scale\u y\u continuous
或coord\u cartesian
?看到这篇文章,你能在一个内置数据集(如cars或iris)上创建一个可复制的例子吗?谢谢你的两个建议,多亏了@missue的解决方案,它才得以实现。
gplot <- function(prd) {
ggplot() +
geom_polygon(data = shp.t,
aes_(x = ~long,
y = ~lat,
group = ~group),
fill = "white",
colour = "grey") +
## Plot errorbar
geom_errorbar(data = te10.cent,
size = 2,
colour = "red",
alpha = .8,
width = 0,
aes_(x = ~long.cent - 350,
ymin = ~lat.cent,
ymax = ~lat.cent + prd))
}
library(tidyverse)
data(iris)
iris %>%
group_by(Species) %>%
summarise_all(~mean(.)) -> summed_iris
gplot <- function(prd){
ggplot(summed_iris) +
geom_col(aes_(x = ~Species,
y = ~Sepal.Length))+
geom_errorbar(aes_(x = ~Species,
ymin = ~Sepal.Length -prd,
ymax = ~Sepal.Length + prd))
}
gplot(0.5)
gplot <- function(prd){
ymin <- with(summed_iris, get("Sepal.Length") - get(prd))
ymax <- with(summed_iris, get("Sepal.Length") + get(prd))
summed_iris <- data.frame(summed_iris, ymin, ymax)
ggplot(summed_iris) +
geom_col(aes_(x = ~Species,
y = ~Sepal.Length))+
geom_errorbar(aes_(x = ~Species,
ymin = ~ymin,
ymax = ~ymax))
}
gplot("Petal.Length")