我尝试构建以下示例图(有关CIELAB色轮的示例,请参阅here)。它已接近完成,但仍有一些小问题(见demo): - 轴上的数字(数字,不确定它是如何用英语调用的)应该直接在x / y轴上(x = 0和y = 0)。目前我正在使用hline和vline来“添加”轴。有没有办法移动整个轴? - 轴标签位于轴旁边,但应位于轴的末端,如样本图像中所示。我不确定是否有解决方案。试图在wickham和chang的书中找到解决方案,但失败了。不确定ggplot2是否可以使用这样的情节。
对不起链接,作为新用户我不允许发布图片:(
非常感谢 西蒙!
library(ggplot2)
circleFun <- function(center = c(0,0),diameter = 1, npoints = 100){
r = diameter / 2
tt <- seq(0,2*pi,length.out = npoints)
xx <- center[1] + r * cos(tt)
yy <- center[2] + r * sin(tt)
return(data.frame(x = xx, y = yy))
}
# sample data for yellow
colorvals <- data.frame(file = 'Yellow.csv', L = 88.94026, a = -9.8599137, b=88.77139)
# build the circles for the plot
r20 <- circleFun(center = c(0, 0), diameter = 40, npoints = 100)
r40 <- circleFun(center = c(0, 0), diameter = 80, npoints = 100)
r60 <- circleFun(center = c(0, 0), diameter = 120, npoints = 100)
r80 <- circleFun(center = c(0, 0), diameter = 160, npoints = 100)
r100 <- circleFun(center = c(0, 0), diameter = 200, npoints = 100)
r120 <- circleFun(center = c(0, 0), diameter = 240, npoints = 100)
dat <- rbind(r20, r40, r60, r80, r100, r120)
# plot the data
ggplot(data = dat, aes(x, y)) +
geom_path() +
geom_hline() +
geom_vline() +
theme(legend.position = c(1,0), legend.justification=c(1,0)) +
xlab("a* (Grün/Rot)") +
ylab("b* (Gelb/Blau)") +
labs(colour="L*") +
geom_point(data = colorvals, aes(x = a, y = b), size=3) +
geom_text(data = colorvals, aes(x = a, y = b, label = gsub(".csv", "", file)), size = 3, vjust=0,hjust=1.2)
答案 0 :(得分:4)
在绘图面板内移动整个轴并不是直截了当的(可以说有充分的理由)。你可以这样做,
g <- ggplotGrob(p)
library(gtable)
# move the axis up in the gtable
g$layout[g$layout$name == "axis-b", c("t", "b")] <-
g$layout[g$layout$name == "panel", c("t", "b")]
# extract the axis gTree and modify its viewport
a <- g$grobs[[which(g$layout$name == "axis-b")]]
a$vp <- modifyList(a$vp, list(y=unit(0.5, "npc")))
g$grobs[[which(g$layout$name == "axis-b")]] <- a
g$layout[g$layout$name == "axis-l", c("l", "r")] <-
g$layout[g$layout$name == "panel", c("l", "r")]
# extract the axis gTree and modify its viewport
b <- g$grobs[[which(g$layout$name == "axis-l")]]
b$vp <- modifyList(b$vp, list(x=unit(0.5, "npc")))
g$grobs[[which(g$layout$name == "axis-l")]] <- b
#grid.newpage()
#grid.draw(g)
## remove all cells but panel
panel <- g$layout[g$layout$name == "panel",]
gtrim <- g[panel$b, panel$r]
## add new stuff
gtrim <- gtable_add_rows(gtrim, unit(1,"line"), pos=0)
gtrim <- gtable_add_rows(gtrim, unit(1,"line"), pos=-1)
gtrim <- gtable_add_cols(gtrim, unit(1,"line"), pos=0)
gtrim <- gtable_add_cols(gtrim, unit(1,"line"), pos=-1)
gtrim <- gtable_add_grob(gtrim, list(textGrob("top"),
textGrob("left", rot=90),
textGrob("right", rot=90),
textGrob("bottom")),
t=c(1,2,2,3),
l=c(2,1,3,2))
grid.newpage()
grid.draw(gtrim)
答案 1 :(得分:0)
除了使用theme(axis.text=element_blank(),axis.ticks=element_blank())
删除旧轴和geom_text()
添加新轴标签,手动创建文本之外,我不知道有什么方法可以做到这一点。我在下面修改了你的代码:
library(ggplot2)
circleFun <- function(center = c(0,0),diameter = 1, npoints = 100){
r = diameter / 2
tt <- seq(0,2*pi,length.out = npoints)
xx <- center[1] + r * cos(tt)
yy <- center[2] + r * sin(tt)
return(data.frame(x = xx, y = yy))
}
# sample data for yellow
colorvals <- data.frame(file = 'Yellow.csv', L = 88.94026, a = -9.8599137, b=88.77139)
# build the circles for the plot
r20 <- circleFun(center = c(0, 0), diameter = 40, npoints = 100)
r40 <- circleFun(center = c(0, 0), diameter = 80, npoints = 100)
r60 <- circleFun(center = c(0, 0), diameter = 120, npoints = 100)
r80 <- circleFun(center = c(0, 0), diameter = 160, npoints = 100)
r100 <- circleFun(center = c(0, 0), diameter = 200, npoints = 100)
r120 <- circleFun(center = c(0, 0), diameter = 240, npoints = 100)
dat <- rbind(r20, r40, r60, r80, r100, r120)
# define labels and coordinates for axes
y.labs <- data.frame(x=rep(0,11),y=seq(-100,100,by=20))
y.text <- as.character(y.labs[,2])
y.text[6] <- ""
x.labs <- data.frame(x=seq(-100,100,by=20),y=rep(0,11))
x.text <- as.character(x.labs[,1])
x.text[6] <- ""
# plot the data
ggplot(data = dat, aes(x, y)) +
geom_path() +
geom_hline() +
geom_vline() +
theme(axis.text=element_blank(),axis.ticks=element_blank(),legend.position = c(1,0), legend.justification=c(1,0)) +
xlab("a* (Grün/Rot)") +
ylab("b* (Gelb/Blau)") +
labs(colour="L*") +
geom_point(data = colorvals, aes(x = a, y = b), size=3) +
geom_text(data = colorvals, aes(x = a, y = b, label = gsub(".csv", "", file)), size = 3, vjust=0,hjust=1.2) +
geom_text(data=x.labs, aes(x = x, y = y,label = x.text),vjust=0,hjust=.5) +
geom_text(data=y.labs, aes(x = x, y = y,label = y.text),vjust=.5,hjust=1)