簡體   English   中英

geom_jitter較小的增加點大小

[英]geom_jitter smaller increasing point size

我試圖每年在Boxplot中繪制一些人作為點,並以點的大小作為年齡。 當我不寫size = Pnr $ Alterpunkt(這是我根據年齡保存點數的列。對於15歲的人來說,這是0.1;對於16歲的人來說,是0.2,依此類推。) aes看起來很完美,但是那時我沒有一個傳奇,我想要一個。 如果我把它寫到aes上,積分會變得更大,而且看起來也不錯,因為我有很多積分。

然后看起來像這樣:

在此處輸入圖片說明

我已經嘗試將形狀設置為“。” 或筆划= 0。 我也嘗試使用guide_legend,但似乎沒有任何改變

這是情節的代碼:

q <- ggplot(Pnr, aes(group=year, x=year, y=Ges))
q <- q + geom_boxplot(outlier.shape = NA)
q <- q + scale_x_continuous(breaks = c(2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2014,
                                       2015,2016,2017,2018,2019,2020))
q <- q + scale_y_continuous(limits = c(-1000,10000))
q <- q + theme(legend.position = c(0.3, 0.3),
               legend.direction = "horizontal", 
               legend.background = element_rect(fill = "white", colour = NA), 
               legend.title=element_blank(), 
               panel.background = element_rect(fill="white", colour="darkgrey", linetype="solid"),
               panel.grid.major = element_line(colour="lightgrey", size=0.1, linetype="solid"),
               panel.grid.minor = element_line(colour="lightgrey", size=0.1, linetype="solid"),
               strip.background = element_rect(fill="lightgrey", colour = "darkgrey"))
q <- q + geom_jitter(aes(size=Pnr$Alterpunkt, shape="."), show.legend = T)

我想讓它看起來像這樣,但有一個傳說

在此處輸入圖片說明

我希望有人能幫助我。

如果沒有數據示例,很難知道該如何提供幫助。 以下是一些建議,希望對您有所幫助:

  • geom_jitter(aes(size=Alterpunkt)...而不是geom_jitter(aes(size=Pnr$Alterpunkt, shape=".") ,因為在ggplot調用中使用$可能會導致錯誤,並且該shape調用應該在aes() ,如果有的話。

  • 使用scale_size_area(max_size =...)scale_size_continuous(range = c(...))定義點的大小。 根據您的審美目標,您可以應用變換來調整大小隨基礎變量的變化而變化的方式。 在下面的情況下,我喜歡使用trans = scales::exp_trans(base = 1.1)的外觀-這使大的點比中號的點大很多,但沒有大得多。 調整口味。

這是一個例子: 在此處輸入圖片說明

library(gapminder)
library(tidyverse)

fake_df <- gapminder %>%
  mutate(age = (lifeExp - 23.5)*1.7,
         Ges = (gdpPercap/1000)^0.5) %>%
  select(continent, year, age, Ges)

q <- ggplot(fake_df, aes(group=year, x=year, y=Ges))
q <- q + geom_boxplot(outlier.shape = NA)
q <- q + scale_x_continuous(breaks = seq(1950, 2020, 10))
# q <- q + scale_y_continuous(limits = c(-1000,10000))
q <- q + theme(legend.position = c(0.3, 0.6),
               legend.direction = "horizontal", 
               legend.background = element_rect(fill = "white", colour = NA), 
               legend.title=element_blank(), 
               panel.background = element_rect(fill="white", colour="darkgrey", linetype="solid"),
               panel.grid.major = element_line(colour="lightgrey", size=0.1, linetype="solid"),
               panel.grid.minor = element_line(colour="lightgrey", size=0.1, linetype="solid"),
               strip.background = element_rect(fill="lightgrey", colour = "darkgrey"))
q <- q + geom_jitter(aes(size=age), show.legend = T) +
  scale_size_continuous(range = c(0.01, 1.5), 
                        trans = scales::exp_trans(base = 1.2),
                        breaks = c(1, 80, 100))
q

暫無
暫無

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

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