亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

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

請問在gggplot 2中如何去除軸與面積之間的空間?

請問在gggplot 2中如何去除軸與面積之間的空間?

蠱毒傳說 2019-08-03 07:03:24
在gggplot 2中如何去除軸與面積之間的空間?我有以下數據:uniq <- structure(list(year = c(1986L, 1987L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L,  2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 1986L, 1987L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 199 7L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 1986L, 198 7L, 1991L, 1992L, 1993L, 1994L, 1995L, 1996L, 1997L, 1998L, 1999L, 2000L, 2001L, 2002L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L,  2010L, 2011L, 2012L, 2013L, 2014L), uniq.loc = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L  , 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,   3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("u.1", "u.2", "u.3  "), class = "factor"), uniq.n = c(1, 1, 1, 2, 5, 4, 2, 16, 16, 10, 15, 14, 8, 12, 20, 11, 17, 30, 17, 21, 22, 19, 34, 44, 56, 11, 0, 0  , 3, 3, 7, 17, 12, 21, 18, 10, 12, 9, 7, 11, 25, 14, 11, 17, 12, 24, 59, 17, 36, 50, 59, 12, 0, 0, 0, 1, 4, 6, 3, 3, 9, 3, 4, 2, 5, 2,當我用:ggplot(data = uniq) +    geom_area(aes(x = year, y = uniq.p, fill = uniq.loc), stat = "identity", position = "stack") +   scale_x_continuous(limits=c(1986,2014)) +   scale_y_continuous(limits=c(0,101)) +   theme_bw()但是,我想刪除軸和實際圖形之間的空間。當我加上theme(panel.grid = element_blank(), panel.margin = unit(-0.8, "lines"))我收到以下錯誤消息:Error in theme(panel.grid = element_blank(), panel.margin = unit(-0.8,  :    could not find function "unit"對于如何解決這個問題,有什么建議嗎?
查看完整描述

3 回答

?
一只萌萌小番薯

TA貢獻1795條經驗 獲得超7個贊

這個問題通過增加expand=c(0,0)scale_x_continuousscale_y_continuous..這也消除了添加panel.margin參數。

守則:

ggplot(data = uniq) + 
  geom_area(aes(x = year, y = uniq.p, fill = uniq.loc), stat = "identity", position = "stack") +
  scale_x_continuous(limits = c(1986,2014), expand = c(0, 0)) +
  scale_y_continuous(limits = c(0,101), expand = c(0, 0)) +
  theme_bw() + theme(panel.grid = element_blank(), panel.border = element_blank())





查看完整回答
反對 回復 2019-08-04
?
MMMHUHU

TA貢獻1834條經驗 獲得超8個贊

截至ggplot2 version 3,有一個expand_scale()函數,您可以將該函數傳遞給expand=參數,該參數允許為縮放的每一側指定不同的展開值。

它還允許您選擇是否希望展開成為絕對大小(使用add=參數)或繪圖大小的百分比(使用mult=參數):

ggplot(data = uniq) + 
  geom_area(aes(x = year, y = uniq.p, fill = uniq.loc), stat = "identity", position = "stack") +
  scale_x_continuous(limits = c(1986,2014), expand = c(0, 0)) +
  scale_y_continuous(limits = c(0,101), expand = expand_scale(mult = c(0, .1))) +
  theme_bw()




查看完整回答
反對 回復 2019-08-04
?
倚天杖

TA貢獻1828條經驗 獲得超3個贊

另一個產生相同結果的選項是使用coord_cartesian而不是連續的位置標度(x&y):

ggplot(data = uniq) +  
  geom_area(aes(x = year, y = uniq.p, fill = uniq.loc), stat = "identity", position = "stack") +  
  coord_cartesian(xlim = c(1986,2014), ylim = c(0,101))+
  theme_bw() + theme(panel.grid=element_blank(), panel.border=element_blank())





查看完整回答
反對 回復 2019-08-04
  • 3 回答
  • 0 關注
  • 588 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號