2017-09-02 74 views
-2

我重新排序GGPLOT2我的價值觀後:爲了填補GGPLOT2重新排序X

KR %>% ggplot(aes(x= reorder(categories, -n), n, fill = categories, order = 
categories)) + geom_bar(stat = "identity") + (axis.title.x=element_blank(), 
axis.text.x=element_blank(), axis.ticks.x=element_blank()) 

現在我想的是fill值具有相同的順序在x軸上的值。我用order嘗試過,但它不起作用。

str(KR) 
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 20 obs. of 2 variables: 
$ categories: chr "Food" "Nightlife" "Bars" "American (Traditional)" ... 
$ n   : int 8576 6334 6067 5312 5250 5229 5220 4118 3868 3673 ... 

Picture of the plot

+0

歡迎棧溢出請重建您的示例以可重現的方式嘗試使用內置數據集請參閱此指南:http://stackoverflow.com/help/mcve和https://stackoverflow.com/questions/5963269/如何對做 - 一個偉大-R-重複性,例如 –

回答

1

做它自己的方式,你可以按照下面的例子:

library(tibble) 
KR <- data_frame(
    categories=c("Food","Nightlife","Bars","American (Traditional)"), 
    n=c(576,6334,6067,5312)) 
str(KR) 
#Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 4 obs. of 2 variables: 
#$ categories: chr "Food" "Nightlife" "Bars" "American (Traditional)" 
#$ n   : num 576 6334 6067 5312 

library(ggplot2) 
KR %>% 
    ggplot(aes(x= reorder(categories, -n), y=n, fill = reorder(categories, -n), order = categories)) + 
    geom_bar(stat = "identity") + 
    theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank()) 

或者我認爲更好的解決方案是創建這也將責令填充有序因素:

KR$categories <- factor(KR$categories, 
         levels=c("Nightlife","Bars","American (Traditional)","Food"), 
         ordered = T) 
KR %>% 
    ggplot(aes(x= categories, y=n, fill = categories)) + 
    geom_bar(stat = "identity") + 
    theme(axis.title.x=element_blank(), axis.text.x=element_blank(), axis.ticks.x=element_blank()) 

enter image description here

0

希望這會讓你朝着正確的方向前進。一旦您滿意的基本圖,你就可以開始調整的主題(例如,主題(axis.title.x = element_blank()...)

suppressPackageStartupMessages(library(tidyverse)) 

# generate example data frame 
KR <- tibble(categories = rep(c("Food", "Nightlife", "Bars", "Other"), each = 3), 
      x = ceiling(runif(12, min = 5000, max = 9000))) 

# build chart 
KR %>% 
    count(categories, wt = x) %>% 
    mutate(categories = reorder(categories, -n)) %>% 
    ggplot(aes(x= categories, n, fill = categories)) + 
    geom_col()