[英]Resize points of scatter plot in ggplot
我有數據:
+-----------+---------+----------+
| AGE_group | mean_y | count_y |
+-----------+---------+----------+
| 1 | 0.141 | 1115 |
| 2 | 0.196 | 1043 |
| 3 | 0.202 | 1093 |
| 4 | 0.114 | 1123 |
+-----------+---------+----------+
我用ggpplot:
g_age <- ggplot(AGE_group_df, aes(AGE_group, mean_y, group = 1)) +
geom_line(size=1, color='blue') +
geom_point(aes(size=AGE_group_df$count_y), color='darkblue') +
labs(x = 'Age Group',
y='% Subscribe',
title='Age Group and Subscribe Rate',
size='# Customer')
g_age
問題:第二點的大小(計數為1043)與其他點相比很小。
問題:如何更改積分大小? (我想使所有4個點的大小幾乎相等),同時保持原始計數單位。
非常感謝你。
您可以使用(至少)三種方法
scale_size_area
: scale_size_area()
scale_size
的range參數: scale_size(range = 4:5)
count_y
: scale_size(limits = c(0, max(AGE_group_df$count_y))
這里更詳細:一種選擇是使用scale_size_area
:
AGE_group_df <- data.frame(AGE_group = 1:4,
mean_y = c(0.141, 0.196, 0.202, 0.114),
count_y = c(1115, 1043, 1093, 1123))
ggplot(AGE_group_df, aes(x = AGE_group, y = mean_y, size = count_y)) +
geom_line(size=1, color='blue') +
scale_size_area(breaks = round(seq(min(AGE_group_df$count_y),
max(AGE_group_df$count_y), length.out = 4), 0)) +
geom_point(color='darkblue') +
labs(x = 'Age Group',
y='% Subscribe',
title='Age Group and Subscribe Rate',
size='# Customer')
另一個在scale_size
手動定義范圍參數的scale_size
:
ggplot(AGE_group_df, aes(x = AGE_group, y = mean_y, size = count_y)) +
geom_line(size=1, color='blue') +
scale_size(breaks = round(seq(min(AGE_group_df$count_y),
max(AGE_group_df$count_y), length.out = 4), 0), range = 4:5) +
geom_point(color='darkblue') +
labs(x = 'Age Group',
y='% Subscribe',
title='Age Group and Subscribe Rate',
size='# Customer')
ggplot(AGE_group_df, aes(x = AGE_group, y = mean_y, size = count_y)) +
geom_line(size=1, color='blue') +
scale_size(breaks = round(seq(min(AGE_group_df$count_y),
max(AGE_group_df$count_y), length.out = 4), 0),
limits = c(0, max(AGE_group_df$count_y))) +
geom_point(color='darkblue') +
labs(x = 'Age Group',
y='% Subscribe',
title='Age Group and Subscribe Rate',
size='# Customer')
請注意,您可以在aes
函數中定義大小。 我添加了休息時間以顯示最小值和最大值,但這不是必需的。 這只是一個額外的壯舉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.