2013-02-04 24 views
0
t <- structure(list(X = 1:30, Country = structure(c(3L, 1L, 3L, 1L, 
3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 3L, 1L, 2L, 2L, 
3L, 1L, 3L, 1L, 3L, 1L, 2L, 3L, 1L, 3L), .Label = c("China", 
"Germany", "USA"), class = "factor"), Industry = structure(c(3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Agriculture", 
"IT", "Manufacturing"), class = "factor"), Date = structure(c(15393, 
15393, 15394, 15394, 15397, 15397, 15398, 15398, 15399, 15399, 
15400, 15400, 15401, 15401, 15404, 15404, 15405, 15405, 15405, 
15405, 15406, 15406, 15407, 15407, 15408, 15408, 15408, 15411, 
15411, 15412), class = "Date"), count = c(4L, 1L, 5L, 1L, 4L, 
1L, 4L, 1L, 8L, 1L, 7L, 1L, 4L, 1L, 4L, 1L, 9L, 1L, 4L, 3L, 4L, 
1L, 4L, 1L, 9L, 1L, 2L, 7L, 1L, 4L)), .Names = c("X", "Country", 
"Industry", "Date", "count"), row.names = c(NA, 30L), class = "data.frame") 

我需要使yaxis比例與該面板中的數據成比例。當我這樣做:根據每個面板上的數據使用facet_grid縮放yaxix

# install.packages("ggplot2", dependencies = TRUE) 
# install.packages("scales", dependencies = TRUE) 
require(ggplot2) 
require(scales) 
p1 <- ggplot(t, aes(Date, count)) + 
    geom_bar(aes(fill=Industry), stat="identity", position="stack") + 
    geom_smooth(method="lm", se=T, size=0.5, colour="yellow") + 
    xlab("Date") + ylab("Number of Input") + 
    facet_grid(Industry~Country, scale="free", margins=T) + 
    theme(legend.position = 'bottom', legend.direction = 'horizontal', legend.title = element_blank(), legend.text = element_text(size=10, face = 'bold')) +theme(axis.title.x = element_text(face="bold", colour="white", size=12), axis.text.x = element_text(angle=90, face="bold", size=10),axis.title.y = element_text(face="bold", colour="white", angle=90, size=10), axis.text.y=element_text(size=10, face="bold"),legend.text = element_text(size=10, face = 'bold'), legend.title = element_blank()) +scale_x_date(breaks = "3 month", minor_breaks = "1 week", labels=date_format("%b-%y"))+ theme(strip.text.x = element_text(size=10, face="bold", colour="navyblue"), strip.background = element_rect(colour="blue", fill="white"))+ ggtitle("Number of Monthly Breaches")+ 
    theme(plot.title=element_text(size=13, colour="white", face="bold")) + ylim(0, max(t$count)) 

y軸的限制是所有面板一樣的,我想這

scales=free_y 

它似乎並不奏效。任何想法如何解決這個問題?

回答

2

上面的答案是好的。我只想構建代碼,因爲它看起來很難維護。我不會混淆情節和主題陳述。

的單獨情節:

p1 <- ggplot(dat, aes(Date, count)) + 
    geom_bar(aes(fill=Industry), stat="identity", position="stack") + 
    geom_smooth(method="lm", se=T, size=0.5, colour="yellow") + 
    facet_grid(Industry~Country, scales="free_y", margins=T) + 
    scale_x_date(breaks = "3 month", minor_breaks = "1 week", 
      labels=date_format("%b-%y")) 

然後主題

mytheme <- theme(legend.position = 'bottom', legend.direction = 'horizontal', 
     legend.title = element_blank(), 
     legend.text = element_text(size=10, face = 'bold')) + 
    theme(axis.title.x = element_text(face="bold", colour="white", size=12), 
     axis.text.x = element_text(angle=90, face="bold", size=10), 
     axis.title.y = element_text(face="bold", 
            colour="white", 
            angle=90, 
            size=10), 
     axis.text.y=element_text(size=10, face="bold"), 
     legend.text = element_text(size=10, face = 'bold'), 
     legend.title = element_blank()) + 
    theme(strip.text.x = element_text(size=10, 
            face="bold", colour="navyblue"), 
     strip.background = element_rect(colour="blue", fill="white"))+ 
    theme(plot.title=element_text(size=13, colour="white", face="bold")) 


    p1 + mytheme 

enter image description here

4

傢伙,你可能試過scales="free_y",但是你的代碼末尾有一個+ ylim(0, max(t$count)。刪除,看看會發生什麼;)

+1

就是這樣。如果代碼格式更清晰,他可能會注意到自己...... – SlowLearner