第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在ggplot 2中顯示堆疊條形圖上的數(shù)據(jù)值

在ggplot 2中顯示堆疊條形圖上的數(shù)據(jù)值

慕桂英546537 2019-06-14 15:24:00
在ggplot 2中顯示堆疊條形圖上的數(shù)據(jù)值我想在ggplot 2中的堆疊條形圖上顯示數(shù)據(jù)值。這是我嘗試的代碼Year      <- c(rep(c("2006-07", "2007-08", "2008-09", "2009-10"), each = 4))Category  <- c(rep(c("A", "B", "C", "D"), times = 4))Frequency <- c(168, 259, 226, 340, 216, 431, 319, 368, 423, 645, 234, 685, 166, 467, 274, 251)Data      <- data.frame(Year, Category, Frequency)library(ggplot2)p <- qplot(Year, Frequency, data = Data, geom = "bar", fill = Category,     theme_set(theme_bw()))p + geom_text(aes(label = Frequency), size = 3, hjust = 0.5, vjust = 3, position =     "stack") 我想在每個部分的中間顯示這些數(shù)據(jù)值。在這方面的任何幫助都將受到高度贊賞。謝謝
查看完整描述

2 回答

?
Qyouu

TA貢獻(xiàn)1786條經(jīng)驗(yàn) 獲得超11個贊

從…ggplot 2.2.0標(biāo)簽可以通過使用position = position_stack(vjust = 0.5)在……里面geom_text.

ggplot(Data, aes(x = Year, y = Frequency, fill = Category, label = Frequency)) +
  geom_bar(stat = "identity") +
  geom_text(size = 3, position = position_stack(vjust = 0.5))

還請注意“position_stack()position_fill()現(xiàn)在,堆棧值按分組的相反順序排列,這使得默認(rèn)堆棧順序與圖例匹配?!?/trans>


的舊版本有效的答案。ggplot:

這里有一種方法,用來計(jì)算條形圖的中點(diǎn)。

library(ggplot2)library(plyr)# calculate midpoints of bars (simplified using comment by @DWin)Data <- ddply(Data, .(Year), 
   transform, pos = cumsum(Frequency) - (0.5 * Frequency))# library(dplyr) ## If using dplyr... # Data <- group_by(Data,Year) %>%#   
    mutate(pos = cumsum(Frequency) - (0.5 * Frequency))# plot bars and add textp <- ggplot(Data, aes(x = Year, y = Frequency)) +
     geom_bar(aes(fill = Category), stat="identity") +
     geom_text(aes(label = Frequency, y = pos), size = 3)


查看完整回答
反對 回復(fù) 2019-06-14
?
隔江千里

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超10個贊

正如Hadley所提到的,與堆疊條形圖中的標(biāo)簽相比,有更有效的方式來傳遞您的消息。事實(shí)上,堆疊的圖表并不是很有效,因?yàn)闂l形圖(每個類別)不共享一個軸,所以比較是很困難的。

在這些實(shí)例中使用兩個圖幾乎總是更好的,共享一個公共軸。在您的示例中,我假設(shè)您希望顯示總體總數(shù),然后顯示每個類別在給定年份中所占的比例。

library(grid)library(gridExtra)library(plyr)# create a new column with proportionsprop <- function(x) x/sum(x)Data <- ddply(Data,"Year",
transform,Share=prop(Frequency))# create the component graphicstotals <- ggplot(Data,aes(Year,Frequency)) + geom_bar(fill="darkseagreen",
stat="identity") + 
  xlab("") + labs(title = "Frequency totals in given Year")proportion <- ggplot(Data, aes(x=Year,y=Share, group=Category, colour=Category)) +
   geom_line() + scale_y_continuous(label=percent_format())+ theme(legend.position = "bottom") + 
  labs(title = "Proportion of total Frequency accounted by each Category in given Year")# bring them togethergrid.arrange(totals,proportion)

如果要添加頻率值,表是最佳格式。


查看完整回答
反對 回復(fù) 2019-06-14
  • 2 回答
  • 0 關(guān)注
  • 2345 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號