R 控制ggplot中的图例

R 控制ggplot中的图例,r,ggplot2,legend,R,Ggplot2,Legend,我编写了代码,使用ggplot为两个类的某个数值变量绘制密度图。但我无法处理传奇的外观。我的代码如下: mu <- ddply(german, "Class", summarise, grp.mean=mean(Credit_amount)) ggplot(german, aes(x=Credit_amount, fill=as.factor(Class))) + geom_histogram(aes(y=..density..), position="identity", alp

我编写了代码,使用ggplot为两个类的某个数值变量绘制密度图。但我无法处理传奇的外观。我的代码如下:

mu <- ddply(german, "Class", summarise, grp.mean=mean(Credit_amount))


ggplot(german, aes(x=Credit_amount, fill=as.factor(Class))) +
  geom_histogram(aes(y=..density..), position="identity", alpha=0.3)+
  geom_density(alpha=0.6)+
  geom_vline(data=mu, aes(xintercept=grp.mean, color= as.factor(Class)),
             size = 2, alpha = 0.6)+
  labs(title="Credit_amount for the Two Classes (Good and Bad Creditors",x="Credit_amount", y = "Density") +
scale_fill_manual(limits=c("1", "0"), labels = c("Bad Creditors", "Good Creditors"), values = c("blue", "red"))
+ labs(fill="Class of Customers")

mu有人投票否决了你,可能是因为如果没有“德语”数据集,你不可能重现你所做的事情。你应该试着翻译你的问题,使用每个人都可以访问的数据集

除了使用数据集mtcars外,这大致就是您所拥有的:

library(plyr)
library(ggplot2)
data(mtcars)
mtcars$cyl = paste0(mtcars$cyl, ' cyl')
mu <- ddply(mtcars, "cyl", summarise, grp.mean=mean(mpg))
ggplot(mtcars, aes(x=mpg, fill=as.factor(cyl))) +
  geom_histogram(aes(y=..density..), position="identity", alpha=0.3)+
  geom_density(alpha=0.6)+
  geom_vline(data=mu, aes(xintercept=grp.mean, color= as.factor(cyl)),
             size = 2, alpha = 0.6)+
  labs(title="Title",x="mpg", y = "Density") +
  scale_fill_manual(limits=c('4 cyl', '6 cyl', '8 cyl'), 
                    labels = c("4 cyl", "6 cyl", '8 cyl'), 
                    values = c("red", "green", 'blue')) + 
  labs(fill="Class of Customers")

mtcars$cyl = factor(mtcars$cyl, levels = c('8 cyl', '6 cyl', '4 cyl'))
mu <- ddply(mtcars, "cyl", summarise, grp.mean=mean(mpg))
ggplot(mtcars, aes(x=mpg, fill=cyl)) +
  geom_histogram(aes(y=..density..), position="identity", alpha=0.3)+
  geom_density(alpha=0.6)+
  geom_vline(data=mu, aes(xintercept=grp.mean, color= cyl),
             size = 2, alpha = 0.6 )+
  labs(title="mtcars example",x="mpg", y = "Density") +
  scale_fill_manual(limits=c('8 cyl', '6 cyl', '4 cyl'), 
                    labels = c("8 cyl", "6 cyl", '4 cyl'), 
                    values = c("red", "green", 'blue')) + 
  labs(fill="Class of Customers")+
  guides(color=FALSE)