ggplot更改填充颜色而不会丢失颜色渐变 [英] ggplot change fill colour without losing colour gradient
问题描述
也许这是一个非常基本的问题,但我是ggplot和R初学者。
我使用这个命令来获取一个barplot:
$ p $ g $ p $ ggplot(data = melt,aes(x = variable,y = value,fill = value))+
geom_bar(width = .8,stat =identity)+
xlab(Samples)+ ylab(Expression)+ ggtitle(Gapdh)+
theme(plot.title = element_text(face =粗体,size = 12))+
theme(axis.text.x = element_text(angle = 45,hjust = 1,size = 10))+
theme(axis.text.y = element_text (大小= 10))
我想改变barplot的颜色,但保持颜色取决于值列。我试过这个,但是我失去了渐变:
ggplot(data = melt,aes(x = variable,y = value ,fill = value))+
geom_bar(width = .8,stat =identity,fill =red)+
xlab(Samples)+ ylab(Expression)+ ggtitle (Gapdh)+
theme(plot.title = element_text(face =bold,size = 12))+
theme(axis.text.x = element_text(angle = 45,hjust = 1,size = 10))+
theme(axis.text.y = element_text(size = 10))
数据很简单,只有两列(变量 - 值):
变量值
1 nu73 13576.49
2 nu73t 10891.88
3 nu81 12673.33
4 nu81t 12159.91
5 nu83 12570.82
6 nu83t 11828.04
预先感谢您
您想调整比例,特别是填充颜色的连续比例,因此函数 scale_fill_continuous()
。
ggplot(data = melt,aes(x = variable,y = value,fill = value))+
geom_bar(width = .8,stat =identity) +
labs(x =Samples,y =Expression,title =Gapdh)+
theme(plot.title = element_text(face =bold,size = 12),
axis.text.x = element_text(angle = 45,hjust = 1,size = 10),
axis.text.y = element_text(size = 10))+
scale_fill_continuous(low = firebrick4,high =firebrick1)
(我稍微修改了您的绘图代码: 主题
一次有多个参数,而且我发现 labs
比一些单独的标记调用要好。)
另一种选择是使用RColorBrewer包中的调色板(它们被合并到 ggplot2
中)。如果对于离散色阶,则可以使用 scale_fill_distiller()
来将它们提取为连续的比例,如果 scale_fill_brewer()
。例如
scale_fill_distiller(type =seq,palette =Reds)
要查看所有可用的比例,请运行 RColorBrewer :: display.brewer.all()
。
Maybe this is a very basic question but I'm a ggplot and R beginner.
I'm using this command to get a barplot:
ggplot(data=melt, aes(x=variable, y=value, fill=value)) +
geom_bar(width=.8, stat="identity") +
xlab("Samples") + ylab("Expression") + ggtitle("Gapdh") +
theme(plot.title=element_text(face="bold", size=12)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1, size=10)) +
theme(axis.text.y = element_text(size=10))
I want to change the colors of barplot, but keeping the gradient of the colors depending on value column. I've tried this but I lose the gradient:
ggplot(data=melt, aes(x=variable, y=value, fill=value)) +
geom_bar(width=.8, stat="identity", fill="red") +
xlab("Samples") + ylab("Expression") + ggtitle("Gapdh") +
theme(plot.title=element_text(face="bold", size=12)) +
theme(axis.text.x = element_text(angle = 45, hjust = 1, size=10)) +
theme(axis.text.y = element_text(size=10))
The data is simple, only two columns( variable - value ):
variable value
1 nu73 13576.49
2 nu73t 10891.88
3 nu81 12673.33
4 nu81t 12159.91
5 nu83 12570.82
6 nu83t 11828.04
Thank you guys in advance
You want to adjust the scale, in particular the continuous scale of fill colors, hence the function scale_fill_continuous()
.
ggplot(data = melt, aes(x = variable, y = value, fill = value)) +
geom_bar(width = .8, stat = "identity") +
labs(x = "Samples", y = "Expression", title = "Gapdh") +
theme(plot.title = element_text(face = "bold", size = 12),
axis.text.x = element_text(angle = 45, hjust = 1, size = 10),
axis.text.y = element_text(size = 10)) +
scale_fill_continuous(low = "firebrick4", high = "firebrick1")
(I slightly modified your plotting code: you can call theme
once with multiple arguments, and I find labs
nicer than a bunch of individual labeling calls.)
One other option is to use the palettes from the RColorBrewer package (which are incorporated into ggplot2
). The scale_fill_brewer()
scale if for discrete color scales, you can "distill" them into continuous scales with scale_fill_distiller()
. For example
scale_fill_distiller(type = "seq", palette = "Reds")
To see all of the available scales, run RColorBrewer::display.brewer.all()
.
这篇关于ggplot更改填充颜色而不会丢失颜色渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!