为什么ggplot在指定其他人时使用默认颜色? [英] Why is ggplot using default colors when others are specified?

查看:135
本文介绍了为什么ggplot在指定其他人时使用默认颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让ggplot2显示直方图的一行,作为与其他颜色不同的颜色。在这我已经成功了;但是,ggplot在指定不同的集合时使用默认颜色。我确信我的代码有错误,但我无法确定它在哪里。数据和代码如下:

创建数据



  library( (round(runif(100000,min = 1.275,max = 1.725),digits = 2))$ b(set_seed(71185)
dist.x< - as.data.frame $ b colnames(dist.x)< - 'sim_con'



开始直方图



  ggplot(dist.x,aes(x = sim_con))+ 
geom_histogram(color =black,aes(fill = ifelse (dist.x $ sim_con == 1.55,darkgreen,firebrick)),binwidth = .01)+
theme(legend.position =none)

结果如下图:

我不想使用默认颜色,而是想使用'darkgreen'和'firebrick'。代码中的错误在哪里?感谢您提供的任何帮助。

解决方案

我不认为您可以在 AES ;您需要在 scale_fill_manual 中执行此操作,如下例所示:

  ggplot(dist.x,aes(x = sim_con))+ 
geom_histogram(color =black,binwidth = .01,aes(fill =(sim_con == 1.55)))+
scale_fill_manual (values = c('TRUE'='darkgreen','FALSE'='firebrick'))+
theme(legend.position =none)
pre>


I am trying to have ggplot2 show one line of a histogram as a different color than the rest. In this I have been successful; however, ggplot is using the default colors when a different set are specified. I am sure there is an error in my code, but I am unable to determine where it is. The data and code are below:

create data

library(ggplot2)
set.seed(71185)
dist.x <- as.data.frame(round(runif(100000, min= 1.275, max= 1.725), digits=2))
colnames(dist.x) <- 'sim_con'

start histogram

ggplot(dist.x, aes(x = sim_con)) +
geom_histogram(colour = "black", aes(fill = ifelse(dist.x$sim_con==1.55, "darkgreen", "firebrick")), binwidth = .01) +
theme(legend.position="none")

Which results in the following image:

I do not want to use the default colors, but instead want to use 'darkgreen' and 'firebrick'. Where is the error in the code? Thanks for any help you can provide.

解决方案

I don't think you can explicitly set colors in aes; you need to do it in scale_fill_manual, as in the example below:

ggplot(dist.x, aes(x = sim_con)) +
  geom_histogram(colour = "black", binwidth = .01,aes(fill=(sim_con==1.55))) + 
  scale_fill_manual(values=c('TRUE'='darkgreen','FALSE'='firebrick')) +
  theme(legend.position="none")

这篇关于为什么ggplot在指定其他人时使用默认颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆