2017-06-27 34 views
1

answering這個question,我遇到一個奇怪的問題;錯誤軸標籤ggplot小格

我想用ggplot2製作條形圖,並將x軸標籤作爲Group列的最後一個字符。使用substring(Group, 3, 3)爲了這個目的:

substring(df$Group, 3, 3) 
# [1] "1" "2" "3" "1" "2" "1" "2" "3" "4" 

但是,當我在ggplot下面一樣使用它,它打印1,而不是4在最後一跳;

ggplot(data=df, aes(x=Group, y=Value)) + 
    geom_bar(stat="identity") + 
    scale_x_discrete(labels = substring(Group, 3, 3), expand=c(0.1,0.1)) + 
    facet_grid(~ substring(Group, 1, 1), space="free_x", scales="free_x", switch="x") + 
    theme_bw() + 
    theme(strip.placement = "outside", 
     strip.background = element_rect(fill=NA,colour="grey50"), 
     panel.spacing=unit(0,"cm")) 

barplot

我可以得到它通過使用labels =unique(substring(Group, 3, 3)工作,但會有人解釋這是怎麼回事?

數據:

df <- structure(list(Group = structure(1:9, .Label = c("1_1", "1_2", 
    "1_3", "2_1", "2_2", "3_1", "3_2", "3_3", "3_4"), class = "factor"), 
     Value = c(-1.23, 2.34, 0.56, 1.87, -2.4, 5.54, -0.98, -2.31, 
     6)), .Names = c("Group", "Value"), row.names = c(NA, -9L), class = "data.frame") 

# > df 

# Group Value 
# 1 1_1 -1.23 
# 2 1_2 2.34 
# 3 1_3 0.56 
# 4 2_1 1.87 
# 5 2_2 -2.40 
# 6 3_1 5.54 
# 7 3_2 -0.98 
# 8 3_3 -2.31 
# 9 3_4 6.00 

回答

2

我認爲,@格雷戈爾的答案是要走的路,但我可以解釋的錯誤。即使您在group之前添加df$,也會得到您所描述的行爲。你可以看到爲什麼你不讓尺度有所不同:

ggplot(data=df, aes(x=Group, y=Value)) + 
    geom_bar(stat="identity") + 
    scale_x_discrete(labels = substring(df$Group, 3, 3), expand=c(0.1,0.1)) + 
    facet_grid(~ substring(Group, 1, 1), switch="x") + 
    theme_bw() + 
    theme(strip.placement = "outside", 
     strip.background = element_rect(fill=NA,colour="grey50"), 
     panel.spacing=unit(0,"cm")) 

給出: enter image description here

正如你所看到的,在這裏它給出了「4」正確。但是,當您直接設置標籤時,如果允許刻度變化,則會分別爲每個刻面設置它們(按順序)。從本質上講,你的標籤設置爲 「1,2,3,1,2,1,2,3,4」 每一次,但僅使用前幾個。

如果你想堅持更接近你的當前,你還需要設置中斷,例如:

ggplot(data=df, aes(x=Group, y=Value)) + 
    geom_bar(stat="identity") + 
    scale_x_discrete(breaks = df$Group, labels = substring(df$Group, 3, 3), expand=c(0.1,0.1)) + 
    facet_grid(~ substring(Group, 1, 1), space="free_x", scales="free_x", switch="x") + 
    theme_bw() + 
    theme(strip.placement = "outside", 
     strip.background = element_rect(fill=NA,colour="grey50"), 
     panel.spacing=unit(0,"cm")) 

其中給出

enter image description here

3

讓我們簡化並在一個新的R對話開始:

df <- structure(list(Group = structure(1:9, .Label = c("1_1", "1_2", 
                 "1_3", "2_1", "2_2", "3_1", "3_2", "3_3", "3_4"), class = "factor"), 
        Value = c(-1.23, 2.34, 0.56, 1.87, -2.4, 5.54, -0.98, -2.31, 
           6)), .Names = c("Group", "Value"), row.names = c(NA, -9L), class = "data.frame") 

library(ggplot2) 
ggplot(data=df, aes(x=Group, y=Value)) + 
    geom_bar(stat="identity") + 
    scale_x_discrete(labels = substring(Group, 3, 3), expand=c(0.1,0.1)) 
# Error in substring(Group, 3, 3) : object 'Group' not found 

scale_x_discrete參數沒有訪問內數據環境。 (我認爲只有兩個地方,你可以放心地在ggplot2引用數據列:內aes()並作爲一個小面呼叫formula的一部分),你的代碼必須從全局的環境,這可能是整列被找到Group對象。這就是爲什麼你沒有在你的問題中遇到錯誤,但是當我使用新的R會話時。

通常使ggplot2做你想做的最簡單的方法是修復數據,以便它匹配你想要什麼:

df$group_last = substring(df$Group, 3, 3) 
ggplot(data=df, aes(x=group_last, y=Value)) + 
    geom_bar(stat="identity") + 
    scale_x_discrete(expand=c(0.1,0.1)) 

在這一點上,你可以添加你的面和主題化回,一切都將工作。

+0

如果我用什麼'$ DF Group'?我試過這個,仍然得到相同的結果。除此之外,我甚至使用了'labels = c(「1」,「2」,「3」,「1」,「2」,「1」,「2」,「3」,「4」)'我再次得到同樣的情節? o.O很奇怪,不是嗎? – Masoud

+1

您有一個具有4個不同值的軸,並且您提供了9個標籤。它使用您提供的前四個標籤。什麼是令人驚訝的? – Gregor

+0

可能是一個天真的問題,但爲什麼我有4個不同的值? – Masoud