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

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

在行尾繪制標(biāo)簽

在行尾繪制標(biāo)簽

撒科打諢 2019-12-20 11:01:30
我有以下數(shù)據(jù)(temp.dat有關(guān)完整數(shù)據(jù),請參見尾注)   Year State     Capex1  2003   VIC  5.3564152  2004   VIC  5.7652323  2005   VIC  5.2472764  2006   VIC  5.5798825  2007   VIC  5.142464...我可以生成以下圖表:ggplot(temp.dat) +   geom_line(aes(x = Year, y = Capex, group = State, colour = State))我希望標(biāo)簽不是傳說,而是顏色與系列相同每個系列的最后一個數(shù)據(jù)點(diǎn)的右側(cè)我在以下鏈接的答案中注意到baptiste的評論,但是當(dāng)我嘗試修改他的代碼(geom_text(aes(label = State, colour = State, x = Inf, y = Capex), hjust = -1))時,文本不會出現(xiàn)。ggplot2-在情節(jié)之外進(jìn)行注釋temp.dat <- structure(list(Year = c("2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014", "2003", "2004", "2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012", "2013", "2014"), State = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("VIC", "NSW", "QLD", "WA"), class = "factor"), Capex = c(5.35641472365348, 5.76523240652641, 5.24727577535625, 5.57988239709746, 5.14246402568366, 4.96786288162828, 5.493190785287, 6.08500616799372, 6.5092228474591, 7.03813541623157, 8.34736513875897, 9.04992300432169, 7.15830329914056, 7.21247045701994, 7.81373928617117, 7.76610217197542, 7.9744994967006, 7.93734452080786, 8.29289899132255, 7.85222269563982, 8.12683746325074, 8.61903784301649, 9.7904327253813, 9.75021175267288, 8.2950673974226, 6.6272705639724, 6.50170524635367, 6.15609626379471, 6.43799637295979, 6.9869551384028, 8.36305663640294, 8.31382617231745, 8.65409824343971,
查看完整描述

3 回答

?
德瑪西亞99

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

要使用Baptiste的想法,您需要關(guān)閉剪輯。但是,當(dāng)您這樣做時,就會得到垃圾。此外,您需要geom_text隱藏圖例,并為(選擇)2014年的資本支出(Capex),并增加邊距為標(biāo)簽留出空間。(或者,您可以調(diào)整hjust參數(shù)以在打印面板內(nèi)移動標(biāo)簽。)類似以下內(nèi)容:


library(ggplot2)

library(grid)


p = ggplot(temp.dat) + 

  geom_line(aes(x = Year, y = Capex, group = State, colour = State)) + 

  geom_text(data = subset(temp.dat, Year == "2014"), aes(label = State, colour = State, x = Inf, y = Capex), hjust = -.1) +

  scale_colour_discrete(guide = 'none')  +    

  theme(plot.margin = unit(c(1,3,1,1), "lines")) 


# Code to turn off clipping

gt <- ggplotGrob(p)

gt$layout$clip[gt$layout$name == "panel"] <- "off"

grid.draw(gt)

在此處輸入圖片說明


但是,這是最適合的情節(jié)directlabels。


library(ggplot2)

library(directlabels)


ggplot(temp.dat, aes(x = Year, y = Capex, group = State, colour = State)) + 

  geom_line() +

  scale_colour_discrete(guide = 'none') +

  scale_x_discrete(expand=c(0, 1)) +

  geom_dl(aes(label = State), method = list(dl.combine("first.points", "last.points"), cex = 0.8)) 

在此處輸入圖片說明


編輯 要增加端點(diǎn)和標(biāo)簽之間的間距,請執(zhí)行以下操作:


ggplot(temp.dat, aes(x = Year, y = Capex, group = State, colour = State)) + 

  geom_line() +

  scale_colour_discrete(guide = 'none') +

  scale_x_discrete(expand=c(0, 1)) +

  geom_dl(aes(label = State), method = list(dl.trans(x = x + 0.2), "last.points", cex = 0.8)) +

  geom_dl(aes(label = State), method = list(dl.trans(x = x - 0.2), "first.points", cex = 0.8)) 


查看完整回答
反對 回復(fù) 2019-12-20
?
qq_花開花謝_0

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

這個問題雖然古老,但卻是黃金,我為疲倦的ggplot人士提供了另一個答案。


該解決方案的原理可以相當(dāng)普遍地應(yīng)用。


Plot_df <- 

  temp.dat %>% mutate_if(is.factor, as.character) %>%  # Who has time for factors..

  mutate(Year = as.numeric(Year))

現(xiàn)在,我們可以子集數(shù)據(jù)了


ggplot() + 

geom_line(data = Plot_df, aes(Year, Capex, color = State)) +

geom_text(data = Plot_df %>% filter(Year == last(Year)), aes(label = State, 

                                                           x = Year + 0.5, 

                                                           y = Capex, 

                                                           color = State)) + 

          guides(color = FALSE) + theme_bw() + 

          scale_x_continuous(breaks = scales::pretty_breaks(10))

最后的pretty_breaks部分只是用于固定下面的軸。


查看完整回答
反對 回復(fù) 2019-12-20
  • 3 回答
  • 0 關(guān)注
  • 721 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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