我不明白为什么我的代码不起作用:
if(formula=="ValueA"){
df<-melt(f,id =c("ValueA"))
colnames(df)[colnames(df) == 'variable'] <- 'Time'
if(yaxis=="YA"){
colnames(df)[colnames(df) == 'value'] <- 'YA'
pl<- ggplot(df, aes(Time, YA, fill=ValueA))}
if(yaxis=="YB"){
colnames(df)[colnames(df) == 'value'] <- 'YB'
pl<- ggplot(df, aes(Time, YB, fill=ValueA))}
if(yaxis=="YC"){
colnames(df)[colnames(df) == 'value'] <- 'YC'
pl<- ggplot(df, aes(Time, YC, fill=ValueA))}
if(yaxis=="YD"){
colnames(df)[colnames(df) == 'value'] <- 'YD'
pl<- ggplot(df, aes(Time, YD, fill=ValueA))}
if(type=="line"){
pl + geom_line(aes(color=ValueA,group=ValueA)) +theme(
axis.text.x = element_text(angle = 90, hjust = 1))+ ggtitle(paste(title))+ YD theme( panel.border = element_rect(colour = "black", fill=NA, size=1),legend.direction ="vertical",legend.position = "right")+guides(color=guide_legend(ncol=1))+scale_y_continuous(labels=comma)
}
if(type=="bar"){
pl + geom_bar(stat="identity") +theme(
axis.text.x = element_text(angle = 90, hjust = 1))+ ggtitle(paste(title))+ YD theme( panel.border = element_rect(colour = "black", fill=NA, size=1),legend.direction ="vertical",legend.position = "right")+guides(fill=guide_legend(ncol=1))+scale_y_continuous(labels=comma)
}
}
if(formula=="ValueB"){
df<-melt(f,id =c("ValueB"))
colnames(df)[colnames(df) == 'variable'] <- 'Time'
if(yaxis=="YA"){
colnames(df)[colnames(df) == 'value'] <- 'YA'
pl<- ggplot(df, aes(Time, YA, fill=ValueB))}
if(yaxis=="YB"){
colnames(df)[colnames(df) == 'value'] <- 'YB'
pl<- ggplot(df, aes(Time, YB, fill=ValueB))}
if(yaxis=="YC"){
colnames(df)[colnames(df) == 'value'] <- 'YC'
pl<- ggplot(df, aes(Time, YC, fill=ValueB))}
if(yaxis=="YD"){
colnames(df)[colnames(df) == 'value'] <- 'YD'
pl<- ggplot(df, aes(Time, YD, fill=ValueB))}
if(type=="line"){
pl+ geom_line(aes(color=ValueB,group=ValueB))+theme(
axis.text.x = element_text(angle = 90, hjust = 1))+ ggtitle(paste(title))+ YD theme( panel.border = element_rect(colour = "black", fill=NA, size=1),legend.direction ="vertical",legend.position = "right")+guides(color=guide_legend(ncol=1)) +scale_y_continuous(labels=comma)
}
if(type=="bar"){
pl + geom_bar(stat="identity")+theme(
axis.text.x = element_text(angle = 90, hjust = 1))+ ggtitle(paste(title))+ YD theme( panel.border = element_rect(colour = "black", fill=NA, size=1),legend.direction ="vertical",legend.position = "right")+guides(fill=guide_legend(ncol=1)) +scale_y_continuous(labels=comma)
}
}
print(pl)
}
Plotsf<-Plots(Df,"Title","line","YA")
问题是它没有读取我声明想要一条线条或条形图的行下面的pl +
,它只打印一个只有x和y名称的空图形轴。通过打印整个
pl<- ggplot(df, aes(Time, YD, fill=ValueA))+ geom_bar(stat="identity") +theme(
axis.text.x = element_text(angle = 90, hjust = 1))+ ggtitle(paste(title))+ YD theme( panel.border = element_rect(colour = "black", fill=NA, size=1),legend.direction ="vertical",legend.position = "right")+guides(fill=guide_legend(ncol=1))+scale_y_continuous(labels=comma)
输出很完美。有人可以解释为什么pl +
不起作用?
感谢
答案 0 :(得分:0)
所以答案是继承变量,而不仅仅是写作:
pl + ... added stuff
无处可去......请改用:
pl <- pl + ... added stuff