ggplot2中的文本替换和格式化

时间:2015-05-05 22:07:49

标签: r ggplot2

我在ggplot2中的文本替换和格式化方面有点困难。这是我的剧本:

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"))

产生这个情节:

enter image description here

现在:

  1. 如何通过“A”,“B”和“C”更改文本“modu”,“apl”和“mnc”。我以前能够做到这一点,但我似乎无法使用这个数据集。
  2. 另外,

    1. 如何使用与轴值相同的字体格式化A,B,C?
    2. 谢谢!

1 个答案:

答案 0 :(得分:4)

所以,多亏了这里和那里聚集的评论者和点点滴滴,我设法得到了我想要的东西。

这是脚本:

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"))

enter image description here

谢谢!