如何在此上下文中添加图例?

问题描述 投票:0回答:1

Highest yearly value


songs %>% group_by(year) %>% summarise(count=nth(pop,1))%>%
  ggplot(aes(x=factor(year),y=count,fill=year))+geom_bar(stat ='identity' )+theme_classic()

1。如何调整图例以显示年份(2010:2019),而不是现在显示的图例?2.Scale_size_manual不起作用。

r ggplot2 visualization kaggle
1个回答
0
投票

您需要每次(或在外部)将year设置为一个因子,而不仅仅是一次。我没有您的数据,因此我将使用mtcars

library(ggplot2)
library(dplyr)

# first plot
mtcars %>%
  ggplot(aes(factor(carb), disp, fill=carb)) +
  geom_bar(stat="identity")

# second plot
mutate(mtcars, carb = factor(carb)) %>%
  ggplot(aes(carb, disp, fill=carb)) +
  geom_bar(stat="identity")

# alternate code for second plot, not shown
mtcars %>%
  ggplot(aes(factor(carb), disp, fill=factor(carb))) +
  # both     ^^^^^^        and        ^^^^^^
  geom_bar(stat="identity")

two ggplots

((有多种方法可以转换为因子。我在这里使用dplyr,但可以很容易地以base或data.table进行。]

我上面包含“替代”代码,该代码显示手册factor应用于carb的每次使用;在我看来,这不是首选的方法,因为如果您要多次执行此操作,则只需在绘图前执行一次,然后多次使用即可。如果同时需要序号year和数字版本,则可以添加一个新字段,例如ordinal_year=factor(year)

© www.soinside.com 2019 - 2024. All rights reserved.