Reputation: 43
in a R scatterplot I want to give the Y-Axis labels colours and make part bold, other parts italic. I start from a very simple plotting script, where everything is done, except the making the font bold, italic and giving it a colour:
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()
Question: How can I make the textA bold and coloured (red) and at the same time make the textB just italic, not bold, standard colour (black).
Thanks so much.
Upvotes: 2
Views: 1456
Reputation: 3604
I would use axis
for both textA
and textB
, then play around with line
until you have an acceptable spacing.
EDIT: I don't know how to have different colours and fonts within a label. As a workaround, you can put both texts closely together by using text
with a variable x-position. If you resize the window, you might have to adjust the offsets.
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)
Upvotes: 1
Reputation: 83215
I would do that with ggplot2
as follows:
# 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))
The result:
Upvotes: 1