相关疑难解决方法(0)

在R中,如何在ggplot2的scale_fill_brewer中更改一个值的颜色值?

我有一个R数据帧(df),我将其绘制为ggplot2中的条形图,并基于dataframe(df$type)中的列进行着色.现在,我使用默认的着色模式(scale_fill_brewer)来分配颜色.

如何将黑色分配给一个值(scale_fill_brewer)并使用scale_fill_brewer分配其余颜色?(所有其他df $类型都是从1到X的整数集合,其中X是唯一值的数量)

到目前为止,我已经能够通过计算出scale_fill_brewer用于N个不同项目的颜色集然后预先设置颜色黑色并将其传递给我来手动执行此操作df$type == -1.

rhg_cols1<- c("#000000","#F8766D","#7CAE00","#00BFC4","#C77CFF" )
ggplot(y=values,data=df, aes(x=name, fill=factor(type))) + 
  geom_bar()+ scale_fill_manual(values = rhg_cols1)
Run Code Online (Sandbox Code Playgroud)

问题是我需要一个无需手动分配颜色的解决方案,使用十六进制颜色计算器来计算scale_fill_brewer的十六进制值.

就像是:

ggplot(y=values,data=df, aes(x=name, fill=factor(type))) +
  geom_bar()+ scale_fill_brewer(value(-1, "black")
Run Code Online (Sandbox Code Playgroud)

谢谢!

编辑:该解决方案必须适用于30多种颜色,适用于ColorBrewer的"Set2"

r colors ggplot2 colorbrewer

21
推荐指数
2
解决办法
2万
查看次数

如何在ggplot2中扩展调色板

大家好我在ggplot2中制作图形,但我希望为它添加不同的颜色.一种方法是使用RColorBrewer包中的调色板.我使用了这个包中的调色板,但我得到了这个.我使用的代码是下一个:

library(ggplot2)
library(reshape2)
library(scales)
library(RColorBrewer)
#Graphic
png(filename = "Atotal.png", width = 1050, height = 900)
Atotal=ggplot(Melt.Atotal, aes(x = Var2, y = value, fill = Mes)) +
  geom_bar(stat = "identity") + scale_y_continuous(labels = comma) + scale_fill_brewer(palette="Set3") +
  annotate("text", x = ncol(AtotalM) - 1.3, y = finalValA1 + 4000000, 
           label = finalValA1f, size = 5) + theme(axis.text.x=element_text(angle=90,colour="grey20",face="bold"),axis.text.y=element_text(colour="grey20",face="bold",hjust=1,vjust=0.8),axis.title.x=element_text(colour="grey20",face="bold"),axis.title.y=element_text(colour="grey20",face="bold"))+xlab('Mes')+ylab('Total')+ ggtitle("Consumo Automotriz (A)")+theme(plot.title = element_text(lineheight=3, face="bold", color="black",family="F",size=24))
print(Atotal)
dev.off()
Run Code Online (Sandbox Code Playgroud)

scale_fill_brewer(palette="Set3")在此代码中使用的结果是下一个:

在此输入图像描述

并非所有价值观Mes都在考虑之中.我的数据是Melt.Atotal,它有3个变量Mes,Var2Value.构建的代码 Melt.Atotal …

r ggplot2

7
推荐指数
1
解决办法
4985
查看次数

在R中调用多边形时取消重画

我想在R中的单个绘图上绘制60,000+个不重叠的三角形(非结构化三角形网格的一部分)。当前,每个绘图需要15-20分钟,因此无法使用它制作动画。例如,

n <- 100 #Except replace this with 60,000
x <- matrix(runif(3*n), n)
y <- matrix(runif(3*n), n)
cols <- heat.colors(n)[order(x[,1])]
poly <- function(i) {polygon(x[i,], y[i,], col=cols[i])}
plot(0, xlim=c(min(x),max(x)), ylim=c(min(y),max(y)))
sapply(1:n, poly)
Run Code Online (Sandbox Code Playgroud)

是否可以禁止在每个多边形之后重绘polygon()?我猜这是最耗时的步骤,并且在手册页中没有提到。如何实现这一目标的替代建议将不胜感激。谢谢。

graphics r

4
推荐指数
1
解决办法
204
查看次数

标签 统计

r ×3

ggplot2 ×2

colorbrewer ×1

colors ×1

graphics ×1