2016-10-02 31 views
0

我試圖改變一個多變量圖的標籤中ggplot 我的第一個圖像是這樣的: enter image description hereR中ggplot如何更改標籤多個barplots

第一張圖片顯示的是我有兩個變量 - 數量和總毛 我有X標記的G和N(這是我在數據變量值) 我想改變的X標籤更具描述

我怎麼可以更新我的ggplot聲明引入這些新標籤

test %>% 
    group_by(DiscInd) %>% 
    summarise(Count=n(),TotalGross=sum(Gross)/100000000) %>% 
    gather(Var, Val, -DiscInd) %>% 
    ggplot(., aes(x=DiscInd, y = Val, fill=Var)) + 
     geom_bar(stat="identity", position="dodge") + 
    xlab("Year vs Released Difference") + 
    ylab("Total Gross")     + 
    ggtitle("Total Movie with Gross ") 

這可能嗎?

下面是一些記錄,我的數據幀測試

 DiscInd  Gross 
      N  2783918982 
      N  2207615668 
      N  1670328025 
      N  1519479547 
      G  1514019071 
      G  1404705868 

更新: 還我試圖改變標籤和格式標籤,這樣不會互相重疊。

+0

製作'DiscInd'你想要的標籤的一個因素(在'aes',如果你不不想改變你的data.frame),或在'scale_x_discrete'中設置'breaks'和'labels'。 – alistaire

+0

@alistare,scale_x_discrete能夠做到這一點 –

回答

1

比方說,你新的描述標籤名稱是標籤摹說明標誌n說明,這是你想要的嗎?

test %>% 
    group_by(DiscInd) %>% 
    summarise(Count=n(),TotalGross=sum(Gross)/100000000) %>% 
    gather(Var, Val, -DiscInd) %>% 
    ggplot(., aes(x=DiscInd, y = Val, fill=Var)) + 
    geom_bar(stat="identity", position="dodge") + 
    scale_x_discrete(labels=c("Label G Desc", "Label N Desc")) + 
    xlab("Year vs Released Difference") + 
    ylab("Total Gross")     + 
    ggtitle("Total Movie with Gross ") 

x軸的文字與角度:

enter image description here

test %>% 
    group_by(DiscInd) %>% 
    summarise(Count=n(),TotalGross=sum(Gross)/100000000) %>% 
    gather(Var, Val, -DiscInd) %>% 
    ggplot(., aes(x=DiscInd, y = Val, fill=Var)) + 
    geom_bar(stat="identity", position="dodge") + 
    scale_x_discrete(labels=c("Label G Desc", "Label N Desc")) + 
    xlab("Year vs Released Difference") + 
    ylab("Total Gross")     + 
    ggtitle("Total Movie with Gross ") + 
    theme(axis.text.x = element_text(angle = 60, size=15, hjust = 1)) 

enter image description here

+0

我們如何對標籤進行角度定位,對於長名稱,是否有角度parm我可以使用 –