R 在ggplot2中打印时,将合并具有相同名称的因子级别

R 在ggplot2中打印时,将合并具有相同名称的因子级别,r,ggplot2,R,Ggplot2,我有一个具有这种结构的数据帧: > str(pd) 'data.frame': 144 obs. of 3 variables: $ Site : Factor w/ 12 levels "BED","BEU","EB",..: 8 9 10 3 11 1 6 7 5 4 ... $ Month : Factor w/ 12 levels "Dec","Jan","Feb",..: 1 1 1 1 1 1 1 1 1 1 ... $ Density: int 20 20

我有一个具有这种结构的数据帧:

> str(pd)
'data.frame':   144 obs. of  3 variables:
 $ Site   : Factor w/ 12 levels "BED","BEU","EB",..: 8 9 10 3 11 1 6 7 5 4 ...
 $ Month  : Factor w/ 12 levels "Dec","Jan","Feb",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ Density: int  20 20 20 20 20 20 20 20 20 20 ...  
我想在使用ggplot2实际绘图之前,在不更改原始数据集的情况下,将
月份的12个级别的名称从
十二月、一月、二月、…、十一月
更改为
D、J、F、…、N

我尝试使用
plyr
包中的
mapvalues()
,如下所示:

pd$Month2 <- mapvalues(pd$Month , 
                       from = c("Dec","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov"), 
                       to = c("D","J","F","M","A","M","J","J","A","S","O","N"))  
当我在ggplot2中绘制时,正如预期的那样,我只绘制了8个级别,这在本例中是错误的

ggplot(pd,aes(x=Month2,y=Density,fill=Site))+
  geom_bar(stat="identity",position="dodge", fill= "darkgray")+
  xlab("Month")+ylab("Number of Individuals")+
  facet_wrap(~Site,nrow=3)
像一月、六月和七月这样的月份,从同一个字母J开始,在J下只合并一次


如何单独绘制所有重复字母?

为什么不使用原始变量
Dec,Jan,Feb,…
绘制数据,然后只更改x轴标签以显示
D,J,F,…
?在这种情况下,您不会错过任何因子级别。检查这里的示例:尤其是在使用
scale\u x\u离散(“Cut”,labels=c(“Fair”=“F”,“Good”=“G”,“Very Good”=“VG”,“Perfect”=“P”,“Ideal”=“I”))的页面中间(非常感谢您提出这个想法。它工作得很好。
ggplot(pd,aes(x=Month2,y=Density,fill=Site))+
  geom_bar(stat="identity",position="dodge", fill= "darkgray")+
  xlab("Month")+ylab("Number of Individuals")+
  facet_wrap(~Site,nrow=3)