R ggplot百分比乘以100

R ggplot百分比乘以100,r,ggplot2,R,Ggplot2,我想用ggplot做一个图,显示yaxis上的百分比。 这是我使用的数据和代码 data<-"yrs.1.17 yrs.18.44 yrs.45.64 yrs.65.84 yrs.85. 1 4.53 35.12 32.93 22.86 4.57 2 4.01 34.74 34.19 22.71 4.34 3 4.75 33.23 35.19 22.28 4.51 4

我想用ggplot做一个图,显示yaxis上的百分比。 这是我使用的数据和代码

data<-"yrs.1.17 yrs.18.44 yrs.45.64 yrs.65.84 yrs.85.
1     4.53     35.12     32.93     22.86    4.57
2     4.01     34.74     34.19     22.71    4.34
3     4.75     33.23     35.19     22.28    4.51
4     4.60     34.04     36.93     20.70    3.56
5     4.80     33.82     37.69     19.97    3.52
6     4.30     35.09     37.08     19.83    3.65
7     3.65     38.08     36.19     19.18    2.85
8     3.72     38.11     36.10     19.22    2.86"

mydata<- read.table(text=data, header=TRUE)


year<-c(2006,2007,2008,2009,2010,2011,2012,2013)
df<-data.frame(year,mydata)
library(ggplot2)
library(reshape2)
library(scales)
newdf<-melt(df,'year')
ggplot(newdf,aes(x=year,y=value,group=variable,color=variable))+ geom_line(size=1)+
scale_x_continuous(breaks=year)+xlab("Year")+scale_y_continuous(labels = percent,limits=c(1,50),breaks=seq(5,50,by=5))+ylab("Age groups")+
geom_point(aes(shape=variable),size=3)+
ggtitle("Age groups of people between 2006 and 2013")+
theme(legend.title=element_blank(), legend.justification=c(0.6,0.6), legend.position=c(0.95,0.95), legend.text = element_text(size=9), 
axis.text = element_text(size=9), axis.title = element_text(size=9), plot.title=element_text(size = 9))     
数据只需除以100:

ggplot(newdf,
  aes(
    x = year,
    y = value / 100, # here divide by 100
    group = variable,
    color = variable
  )
) + 
  geom_line(size=1)+
  scale_x_continuous(breaks=year) + 
  xlab("Year") + 
  scale_y_continuous(
    labels = percent,
    limits = c(.01, .5),            # here divide by 100
    breaks = seq(.05, .5, by = .05) # here divide by 100
  ) +
  ylab("Age groups") +
  geom_point(aes(shape = variable), size=3) +
  ggtitle("Age groups of people between 2006 and 2013") +
  theme(legend.title=element_blank(),
        legend.justification = c(0.6, 0.6),
        legend.position = c(0.95, 0.95),
        legend.text = element_text(size = 9), 
        axis.text = element_text(size = 9),
        axis.title = element_text(size = 9),
        plot.title = element_text(size = 9)) 

作为旁注,大多数主题都使用
基本大小
参数,而不是
元素文本(大小=9)
的所有手动设置,因此使用
主题灰(9)
主题经典(9)
将为您完成大部分工作。您可能仍然需要调整标题。

我已尝试在代码中使用以下内容,但没有任何更改。scale_y_continuous(labels=percent)scale_y_continuous(labels=scales::percent)scale_y_continuous(labels=percent_format())是第一个问题(plus1)的好例子。如果Gregor回答了您的问题,您可以通过单击旁边的勾号来标记它(您也可以点击那里的。如果Gregor的建议不起作用,快速修复方法是
labels=function(x)paste0(x,“%”)
在使用
y=cumsum(…count…)时可能需要使用
y=cumsum(…count…)
,因为在这种情况下除以100似乎不起作用。