示例代码和图:
data <- data.frame( ID = c(LETTERS[1:26], paste0("A",LETTERS[1:26])),
Group = rep(c("Control","Treatment"),26),
x = rnorm(52,50,20),
y = rnorm(52,50,10))
ggplot(data, aes(y=y,x=x, label=ID, color=Group)) +
geom_text(size=8) +
scale_color_manual(values=c("blue","red")) +
theme_classic() +
theme(legend.text = element_text(color=c("blue","red")))
Run Code Online (Sandbox Code Playgroud)
我正在尝试解决的是删除图例符号("a")并为组标签(控制和处理)着色,因为它们出现在图中(分别为蓝色和红色).
我试过了:
geom_text(show_guide = F)
Run Code Online (Sandbox Code Playgroud)
但这完全取消了传说.
为了保持简单,我可以使用注释...但是想知道是否有特定于图例的解决方案.
ggplot(data, aes(y=y,x=x, label=ID, color=Group)) +
geom_text(size=8, show_guide=F) +
scale_color_manual(values=c("blue","red")) +
theme_classic() +
annotate("text",label="Control", color="blue",x=20,y=80,size=8) +
annotate("text",label="Treatment", color="Red",x=23,y=77,size=8)
Run Code Online (Sandbox Code Playgroud) R 中是否有一种快速方法可以根据纬度间隔或箱对栅格进行汇总统计。不是整个栅格图层的摘要,而是空间分段的摘要。例如,获取纬度每两度的栅格像元值的平均值和标准差。
下面是带有纬度/经度坐标的投影栅格的一些示例数据。
set.seed(2013)
library(raster)
r <- raster(xmn=-110, xmx=-90, ymn=40, ymx=60, ncols=40, nrows=40)
r <- setValues(r, rnorm(1600)) #add values to raster
r[r > -0.2 & r < 0.2] <- NA #add some NA's to resemble real dataset
plot(r)
> r
class : RasterLayer
dimensions : 40, 40, 1600 (nrow, ncol, ncell)
resolution : 0.5, 0.5 (x, y)
extent : -110, -90, 40, 60 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0
data source : in memory
names …Run Code Online (Sandbox Code Playgroud) 是否有解决方案来控制ggplot2中主要网格线的长度?我在这里有一个图,我希望主网格线出现在零线的左侧(只是负值)。因此,对于条“ a”,只有一小部分网格线可见。
# Some Data
df <- data.frame(trt = c("a", "b", "c"),
outcome = c(-222.3, 121.9, 103.2))
# A plot with major grid lines
ggplot(df, aes(trt, outcome)) +
geom_col() +
coord_flip() +
theme_classic()+
theme(panel.grid.major.y = element_line(color = "blue", size = 1)) +
geom_hline(yintercept = 0, size = 1)
Run Code Online (Sandbox Code Playgroud)