2016-03-12 645 views
1

我無法用ggplot2重現一個簡單的圖。我的目標是顯示x和y軸。這個從網站上取得的基本例子不起作用,我不明白爲什麼。任何洞察力高度讚賞!ggplot2 theme_classic顯示x和y軸

library(ggplot2) 
df <- data.frame(x = 1:3, y = 1:3) 
ggplot(df, aes(x, y)) + geom_point()+ theme_classic() + ggtitle("theme_classic()") 

我也使用

theme(axis.line = element_line(colour = "grey50")) 

嘗試,但我也有同樣的問題,即我得到這個 enter image description here

而不是我們所期望的(例如,對於不同的數據集,關鍵是缺少x的和y軸) enter image description here

+1

你是否從cowplot那裏得到了這個? 'theme_classic'給你提供什麼文檔http://docs.ggplot2.org/current/ggtheme.html'ggplot(diamonds,aes(clarity,fill = cut))+ geom_bar()+ cowplot :: theme_cowplot() ' – rawr

+0

這工作在ggplot 2.0.0,但不再在ggplot 2.1.0中工作。儘管如此,您仍然可以通過添加'+ theme(axis.line.x = element_line(color =「black」),axis.line.y = element_line(color =「black」))來治癒。也許他們在[theme-defaults.r]中改變了一些東西(https://github.com/hadley/ggplot2/blob/master/R/theme-defaults.r) – lukeA

+1

一個bug:看看[here](http:// stackoverflow.com/questions/35833307/ggplo2-axis-not-showing-after-using-themeaxis-line-element- line/35833548#35833548)和[bug報告](https://github.com/hadley/ggplot2 /問題/ 1565)。 –

回答

0

奇怪的是,我沒有問題讓你的第一個例子像你期待的那樣工作,這是我無法解釋的。但也許你可以嘗試我通常做的事情:

ggplot(df, aes(x, y)) + 
    geom_point() + 
    theme(axis.line = element_line(colour = "black"), 
     panel.grid.major = element_blank(), 
     panel.grid.minor = element_blank(), 
     panel.border = element_blank(), 
     panel.background = element_blank()) 
+0

你是' packageVersion( 「GGPLOT2」)'?它在2.0.0中工作,但不再在2.1.0中工作。 – lukeA

+0

是的,我有軟件包版本2.1.0。謝謝@lukeA。我能怎麼做 ? – Matilde

+1

@Matilde添加'+主題(axis.line.x = element_line(color =「black」),axis.line.y = element_line(color =「black」))''。模板中的默認'axis.line = element_line(color =「black」)'不起作用。 – lukeA

2

Mayby你正在尋找theme_bw()

ggplot(df, aes(x, y)) + geom_point() + theme_bw() 

這裏是theme_classic()

ggplot(df, aes(x, y)) + 
    geom_point() + 
    theme_classic() + 
    theme(
    axis.line.x = element_line(colour = "grey50"), 
    axis.line.y = element_line(colour = "grey50") 
) 

當一個人需要看到一個主題的結構的解決方案,它可以輸出其價值與dput:

dput(theme_classic()) 

這讓您會看到結果列表中的哪些名稱具有正在執行「消隱」的代碼:

theme_classic()[grepl("axis.line", names(theme_classic()))] 

$axis.line 
List of 4 
$ colour : chr "black" 
$ size : NULL 
$ linetype: NULL 
$ lineend : NULL 
- attr(*, "class")= chr [1:2] "element_line" "element" 

$axis.line.x 
list() 
- attr(*, "class")= chr [1:2] "element_blank" "element" 

$axis.line.y 
list() 
- attr(*, "class")= chr [1:2] "element_blank" "element" 
+0

這就是Mac上所需要的,與其他一些報告不同,它沒有繪製任何軸線。 –

+0

請注意,它可能取決於ggplot2的版本。主題()在2.0.0更改很多更新的版本引入了主題() – Thierry

+0

一些小的變化對於我的盒子它是'packageDescription('ggplot2')$版本 [1]「2.1.0」' –