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不起作用。
您需要每次(或在外部)将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")
((有多种方法可以转换为因子。我在这里使用dplyr
,但可以很容易地以base或data.table
进行。]
我上面包含“替代”代码,该代码显示手册factor
应用于carb
的每次使用;在我看来,这不是首选的方法,因为如果您要多次执行此操作,则只需在绘图前执行一次,然后多次使用即可。如果同时需要序号year
和数字版本,则可以添加一个新字段,例如ordinal_year=factor(year)
。