簡體   English   中英

R中的100%堆積條形圖

[英]100% Stacked bar Chart in R

早上好,

我試圖在R中的100%Stacked Bar圖表中獲取這些數據

        Federal Non Federal
2006    46753094    74740716
2007    43397314    74834857
2008    43962330    71051132
2009    42238038    72987898
2010    49546221    75232382
2011    48730233    76333479
2012    49316564    74669993
2013    48198329    75644892
2014    46630540    74783207
2015    46214781    75004771
2016    47625256    73744148

所以它看起來像這樣: 在此輸入圖像描述

我將是第一個承認它肯定不喜歡令人興奮的地圖,但它仍然需要。

我試着按照這里解釋的那樣編寫代碼,但它沒有用。

這就是我做的:

>     g <- ggplot(FedNonFed, aes(FedNonFed))
>     g + geom_bar(aes(fill = FedNonFed), position = "fill")

不是我需要的圖表。

g <- ggplot(FedNonFed, aes(FY))
g + geom_bar(aes(fill = FedNonFed), position = "fill")
g + geom_bar(aes(fill = TotalExpense), position = "fill")

任何幫助,將不勝感激。

你需要融化你的數據。 我稍微更改了一些數據,以便更容易加載,但這應該很容易替換。

library(reshape2)
library(scales)
df = data.frame("Year" = seq(2006,2016,by = 1), "Federal" = seq(1,11,by = 1), "Non Federal" = seq(11,1,by = -1))
dfm = melt(df, id.vars = "Year")
ggplot(dfm,aes(x = Year, y = value,fill = variable)) + 
  geom_bar(position = "fill",stat = "identity") + 
  scale_y_continuous(labels = percent_format())

我對情節進行了一些修改,因此它更接近於excel情節。 唯一不同的是顏色。

ggplot(dfm,aes(x = Year, y = value,fill = variable)) + 
  geom_bar(position = "fill",stat = "identity") + 
  scale_y_continuous(labels = percent_format())+ scale_x_continuous(breaks = 2006:2016,labels= as.character(seq(2006,2016,by = 1)))+
  theme(plot.subtitle = element_text(vjust = 1), 
    plot.caption = element_text(vjust = 1), 
    legend.title = element_blank(), 
    axis.title.x=element_blank(),
    axis.title.y=element_blank(),
    legend.position = "bottom", legend.direction = "horizontal")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM