Changing and formatting text in ggplot2
I am a bit stuck with replacing and formatting text in ggplot2. This is my script:
require(reshape2)
library(ggplot2)
library(RColorBrewer)
library(extrafont)
font_import(pattern = 'Arch')
library(ggplot2)
library(RColorBrewer)
df <- read.csv("https://dl.dropboxusercontent.com/u/73950/moduVSmnc.csv")
breaks1 <- seq(1.85,2.5,by=0.05)
gg <- aggregate(mnc~cut(apl,breaks=breaks1,
labels=format(breaks1[-1],nsmall=2))+modu,df,mean)
colnames(gg)<- c("apl","modu","mnc")
gg$modu <- as.factor(gg$modu)
ggplot(gg) +
geom_tile(aes(x=modu,y=apl,fill=mnc))+
scale_fill_gradientn(colours=rev(brewer.pal(10,"Spectral"))) +
coord_fixed()+
theme(legend.position = "right", axis.ticks = element_blank(),
axis.text.x = element_text(size = 10, angle = 90, vjust = 0.25 , hjust = 1, colour = "grey50", family="Archer-Medium")
,axis.text.y = element_text(size = 10, angle = 0, hjust = 1, colour = "grey50", family="Archer-Medium"))
What this graph produces:
Now:
- How to change the texts "modu", "apl" and "mnc" to "A", "B" and "C". I've been able to do this in the past, but I can't seem to get it to work with this dataset.
Besides,
- How do I format A, B, C with the same font used for axis values?
Thank!
+3
source to share
1 answer
So, thanks to the commenters and pieces put together here and there, I managed to get what I wanted.
Here's the script:
require(reshape2)
library(ggplot2)
library(RColorBrewer)
library(extrafont)
font_import(pattern = 'Arch')
library(ggplot2)
library(RColorBrewer)
df <- read.csv("https://dl.dropboxusercontent.com/u/73950/moduVSmnc.csv")
breaks1 <- seq(1.85,2.5,by=0.05)
gg <- aggregate(mnc~cut(apl,breaks=breaks1,
labels=format(breaks1[-1],nsmall=2))+modu,df,mean)
colnames(gg)<- c("apl","modu","mnc")
gg$modu <- as.factor(gg$modu)
ggplot(gg) +
geom_tile(aes(x=modu,y=apl,fill=mnc))+ labs(x="A", y="B", fill="C") +
theme(axis.title.x = element_text(vjust = 0 , family="Archer-Semibold", colour="grey25", size=12),
axis.title.y = element_text(vjust = 1 , family="Archer-Semibold", colour="grey25", size=12),
legend.title = element_text(vjust = 1 , family="Archer-Semibold", colour="grey25", size=12),
legend.text = element_text(vjust = 1 , family="Archer-Semibold", colour="grey25", size=12)) +
scale_fill_gradientn(colours=rev(brewer.pal(10,"Spectral"))) +
coord_fixed()+
theme(legend.position = "right", axis.ticks = element_blank(),
axis.text.x = element_text(size = 10, angle = 90, vjust = 0.25 , hjust = 1, colour = "grey50", family="Archer-Medium")
,axis.text.y = element_text(size = 10, angle = 0, hjust = 1, colour = "grey50", family="Archer-Medium"))
Thank!
+4
source to share