散点图:颜色和斜体轴标签

时间:2014-03-25 08:59:51

标签: r axis-labels

在R散点图中我想给Y轴标签颜色并使其成为粗体,其他部分为斜体。我从一个非常简单的绘图脚本开始,一切都已完成,除了使字体变粗,斜体并给它一种颜色:

png("test.png", height=800, width=600, pointsize=15)
par(mar=c(5,12,.25,1))
textA=seq(0,100,25)
textB=c("start","intermediate1","intermediate2","intermediate3","stop")
plot(c(0,runif(8),1),c(0,runif(8),1),axes=F,ylab=NA)
axis(1)
axis(2, at=seq(0,1,.25), labels=paste(textA,"-",textB,sep=""),las=2,cex.axis=1.5)
dev.off()

问题:如何使textA变为粗体和彩色(红色),同时使textB只是斜体,而不是粗体,标准颜色(黑色)。

非常感谢。

2 个答案:

答案 0 :(得分:1)

我会用ggplot2执行此操作,如下所示:

# create a dataframe
x <- c(0,runif(8),1)
y <- c(0,runif(8),1)
df <- as.data.frame(cbind(x, y))

# loading ggplot2 package
require(ggplot2)

# creating the plot
ggplot(df, aes(x=x, y=y)) + 
  geom_point(shape = 21, size = 4) +
  scale_x_continuous("x-axis-label") +
  scale_y_continuous("", breaks=c(0,0.25,0.50,0.75,1.00),
                     labels=c("start","intermediate1","intermediate2","intermediate3","stop")) + 
  theme_classic() +
  theme(axis.text.x = element_text(size=14, face="italic"),
        axis.text.y = element_text(size=14, colour = "red", face="bold"),
        axis.title.x = element_text(vjust=-1))

结果:

enter image description here

答案 1 :(得分:1)

我会对axistextA使用textB,然后使用line,直到您有可接受的间距。

编辑:我不知道如何在标签中使用不同的颜色和字体。作为一种解决方法,您可以使用带有可变x位置的text将两个文本紧密地放在一起。如果调整窗口大小,则可能需要调整偏移量。

par(mar=c(5,13,.25,1))
textA=seq(0,100,25)
textB=c("start","intermediate1","intermediate2","intermediate3","stop")
plot(c(0,runif(8),1),c(0,runif(8),1),axes=F,ylab=NA)
axis(1)

# add textA and textB
axis(2, at=seq(0,1,.25), labels=textB, font=3, col.axis=1,las=2,cex.axis=1.5, line=1)
text(x=c(-0.3,-0.55,-0.55,-0.55,-0.3), y=seq(0,1,.25), textA, xpd=NA, col=2, font=2, cex=1.5)

# check which offset we need for textA:
# abline(v=seq(-1,0,.1), xpd=NA, col=2)
# text(,x=seq(-1,0,.1),y=rep(0,11),labels=seq(-1,0,.1),xpd=NA)

updated result