修改现有绘图的颜色比例尺。

huangapple go评论87阅读模式
英文:

ggplot: modify color scale (only) of existing plot

问题

I have created a bar plot having partially recycling some code from a previous project (the data structure is very nearly identical) which (as expected) has resulted in 5 different coloured bars. However, there is an association between the first 3 bars that I would like to draw attention to by colouring them similarly (e.g. pink, pale red and deep red) while keeping the other 2 bars their present/default colours. I have and would like to maintain other specific characteristics of the code such as keeping the black outline around and the bars and having the error bars also be black in colour. I have tried altering different sections of the code to see if there is a specific element that will allow me to manual set the colour of my bars (e.g. fill=c('pink'...) but this has resulted in disastrous changes to other elements of the plot such as moving all of the error bars into the centre of the plot, changing the naming of the elements in the legend and merging multiple bars on top of each other. Is there a simple way for me to change the colour of these 3 bars using my existing code?

My data is as follows:

emean_Sleep_State <- c('WAKE','SWS','REM','WAKE','SWS','REM','WAKE','SWS','REM','WAKE','SWS','REM','WAKE','SWS','REM')
emean_Treatment <- c('Disturbed_Feed','Disturbed_Feed','Disturbed_Feed','Disturbed_Foot','Disturbed_Foot','Disturbed_Foot','Disturbed_Temp','Disturbed_Temp','Disturbed_Temp','Recovery','Recovery','Recovery','UnDisturbed','UnDisturbed','UnDisturbed')

emean_Means <- c(27.68,52.64,21.36,35.38,47.96,15.59,42.28,45.84,6.03,27.56,52.2,22.36,28.62,52.84,21.01)
emean_SE <- c(3.94,2.99,3.08,3.97,3.01,3.1,3.84,2.91,3.03,2.94,2.34,2.51,5.75,2.92,4.18)

emeanframe <- data.frame(emean_Sleep_State,emean_Treatment,emean_Means,emean_SE)

Estimated_Max_Means_emean <- emeanframe$emean_Means+emeanframe$emean_SE
Estimated_Min_Means_emean <- emeanframe$emean_Means-emeanframe$emean_SE

emeanframe <- data.frame(emean_Sleep_State,emean_Treatment,emean_Means,emean_SE,Estimated_Max_Means_emean,Estimated_Min_Means_emean)

realvalueemean <- apply(emeanframe[,c(3,5,6)],2,FUN=backtransANG)
emeanframe$realmean <- realvalueemean[,1]
emeanframe$realmaxmean <- realvalueemean[,2]
emeanframe$realminmean <- realvalueemean[,3]

If it is of relevance, the function is as follows:

backtransANG <- function(ANG_value){
real_value <- (sin(ANG_value/(180/pi)))^2
return(real_value)
}

The code that I used to create my plot is as follows:

emeanplot2 <- ggplot(emeanframe,
aes(x=emean_Sleep_State,
y=realmean,
fill=emean_Treatment))+
geom_col(position='dodge',
color='black')+
geom_errorbar(aes(ymin=realminmean,
ymax=realmaxmean),
color='black',width=0.2,position=position_dodge(width=0.9),linewidth=1)+
scale_x_discrete(limits=c('WAKE','SWS','REM'))+
labs(x='Sleep State',
y= 'Proportion of Behaviour',
color="Type of Night",size=14)+
ylim(0,1)+
guides(fill=guide_legend(title='Type of Night'))+
theme(axis.text.x=element_text(size=12),
axis.text.y=element_text(size=12),
axis.title.x=element_text(size=14),
axis.title.y=element_text(size=14))

I have tried tinkering with elements such as fill=emean_Treatment, and additional elements within geom_col but nothing has yet resulted in my preferred colours even being added to the plot, only a complete mess of the current plot.

英文:

I have created a bar plot having partially recycling some code from a previous project (the data structure is very nearly identical) which (as expected) has resulted in 5 different coloured bars. However, there is an association between the first 3 bars that I would like to draw attention to by colouring them similarly (e.g. pink, pale red and deep red) while keeping the other 2 bars their present/default colours. I have and would like to maintain other specific characteristics of the code such as keeping the black outline around and the bars and having the error bars also be black in colour. I have tried altering different sections of the code to see if there is a specific element that will allow me to manual set the colour of my bars (e.g. fill=c('pink'...) but this has resulted in disastrous changes to other elements of the plot such as moving all of the error bars into the centre of the plot, changing the naming of the elements in the legend and merging multiple bars on top of each other. Is there a simple way for me to change the colour of these 3 bars using my existing code?

My data is as follows:

emean_Sleep_State&lt;-c(&#39;WAKE&#39;,&#39;SWS&#39;,&#39;REM&#39;,&#39;WAKE&#39;,&#39;SWS&#39;,&#39;REM&#39;,&#39;WAKE&#39;,&#39;SWS&#39;,&#39;REM&#39;,&#39;WAKE&#39;,&#39;SWS&#39;,&#39;REM&#39;,&#39;WAKE&#39;,&#39;SWS&#39;,&#39;REM&#39;)
emean_Treatment&lt;-c(&#39;Disturbed_Feed&#39;,&#39;Disturbed_Feed&#39;,&#39;Disturbed_Feed&#39;,&#39;Disturbed_Foot&#39;,&#39;Disturbed_Foot&#39;,&#39;Disturbed_Foot&#39;,&#39;Disturbed_Temp&#39;,&#39;Disturbed_Temp&#39;,&#39;Disturbed_Temp&#39;,&#39;Recovery&#39;,&#39;Recovery&#39;,&#39;Recovery&#39;,&#39;UnDisturbed&#39;,&#39;UnDisturbed&#39;,&#39;UnDisturbed&#39;)

emean_Means&lt;-c(27.68,52.64,21.36,35.38,47.96,15.59,42.28,45.84,6.03,27.56,52.2,22.36,28.62,52.84,21.01)
emean_SE&lt;-c(3.94,2.99,3.08,3.97,3.01,3.1,3.84,2.91,3.03,2.94,2.34,2.51,5.75,2.92,4.18)

emeanframe&lt;-data.frame(emean_Sleep_State,emean_Treatment,emean_Means,emean_SE)

Estimated_Max_Means_emean&lt;-emeanframe$emean_Means+emeanframe$emean_SE
Estimated_Min_Means_emean&lt;-emeanframe$emean_Means-emeanframe$emean_SE

emeanframe&lt;-data.frame(emean_Sleep_State,emean_Treatment,emean_Means,emean_SE,Estimated_Max_Means_emean,Estimated_Min_Means_emean)

realvalueemean&lt;-apply(emeanframe[,c(3,5,6)],2,FUN=backtransANG)
emeanframe$realmean&lt;-realvalueemean[,1]
emeanframe$realmaxmean&lt;-realvalueemean[,2]
emeanframe$realminmean&lt;-realvalueemean[,3]

If it is of relevance, the function is as follows:

  backtransANG&lt;-function(ANG_value){
  real_value&lt;-(sin(ANG_value/(180/pi)))^2
  return(real_value)}

The code that I used to create my plot is as follows:

emeanplot2&lt;-ggplot(emeanframe,
                   aes(x=emean_Sleep_State,
                       y=realmean,
                       fill=emean_Treatment))+
  geom_col(position=&#39;dodge&#39;,
           color=&#39;black&#39;)+
  geom_errorbar(aes(ymin=realminmean,
                    ymax=realmaxmean),
                color=&#39;black&#39;,width=0.2,position=position_dodge(width=0.9),linewidth=1)+
  scale_x_discrete(limits=c(&#39;WAKE&#39;,&#39;SWS&#39;,&#39;REM&#39;))+
  labs(x=&#39;Sleep State&#39;,
       y= &#39;Proportion of Behaviour&#39;,
       color=&quot;Type of Night&quot;,size=14)+
  ylim(0,1)+
  guides(fill=guide_legend(title=&#39;Type of Night&#39;))+
  theme(axis.text.x=element_text(size=12),
        axis.text.y=element_text(size=12),
        axis.title.x=element_text(size=14),
        axis.title.y=element_text(size=14))

I have tried tinkering with elements such as fill=emean_Treatment, and additional elements within geom_col but nothing has yet resulted in my preferred colours even being added to the plot, only a complete mess of the current plot.

答案1

得分: 1

您可以通过添加新的比例尺来替换现有的比例尺(在您的情况下是填充比例尺)。示例:

p <- data.frame(x = LETTERS[1:3], y = 1:3) |
  ggplot(aes(x, y)) + 
  geom_col(aes(fill = x)) +
  ggtitle('旧的填充颜色') +
  scale_fill_manual(values = c(A = 'green', B = 'blue', C = 'red'))

p_new <- p + 
  ggtitle('新的填充颜色') +
  scale_fill_manual(values = c(A = 'green', B = 'blue', C = 'yellow'))

修改现有绘图的颜色比例尺。

英文:

You can replace existing scales (in your case the fill-scale) by adding a new scale. Example:

p &lt;- data.frame(x = LETTERS[1:3], y = 1:3) |&gt;
  ggplot(aes(x, y)) + 
  geom_col(aes(fill = x)) +
  ggtitle(&#39;old fill colours&#39;) +
  scale_fill_manual(values = c(A = &#39;green&#39;, B = &#39;blue&#39;, C = &#39;red&#39;))


p_new &lt;- p + 
  ggtitle(&#39;new fill colours&#39;) +
  scale_fill_manual(values = c(A = &#39;green&#39;, B = &#39;blue&#39;, C = &#39;yellow&#39;))

修改现有绘图的颜色比例尺。

答案2

得分: 1

你可以通过三次应用相同的颜色轻松实现所需的填充效果。以下是柱状图的颜色设置:

custom_colors <- c("pink", "pink", "pink", "coral2", "darkred")

这行代码定义了一个名为custom_colors的向量,指定了柱状图的颜色。这些颜色根据它们在emeanframe数据框中emean_Treatment变量的顺序分配给柱状图。

ggplot()函数中,fill美学仍然映射到emean_Treatment变量。你只需使用scale_fill_manual()来添加颜色信息。

emeanplot2 <- ggplot(emeanframe,
                     aes(x = emean_Sleep_State,
                         y = realmean,
                         fill = emean_Treatment)) +
  geom_col(position = "dodge",
           color = "black") +
  geom_errorbar(aes(ymin = realminmean,
                    ymax = realmaxmean),
                color = "black",
                width = 0.2,
                position = position_dodge(width = 0.9),
                linewidth = 1) +
  scale_x_discrete(limits = c("WAKE", "SWS", "REM")) +
  labs(x = "Sleep State",
       y = "Proportion of Behaviour",
       color = "Type of Night",
       size = 14) +
  ylim(0, 1) +
  guides(fill = guide_legend(title = "Type of Night")) +
  theme(axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 12),
        axis.title.x = element_text(size = 14),
        axis.title.y = element_text(size = 14)) +
  scale_fill_manual(values = custom_colors)
英文:

You can easily achieve the desired fill by applying the same colour three times. Here's how the bars are coloured:

custom_colors &lt;- c(&quot;pink&quot;, &quot;pink&quot;, &quot;pink&quot;, &quot;coral2&quot;, &quot;darkred&quot;)

This line defines a vector custom_colors that specifies the colours for the bars. These colours are assigned to the bars based on their order in the emean_treatment variable in the emeanframe data frame.

In the ggplot() function, the fill aesthetic is still mapped to the emean_Treatment variable. You just add the information about the colours with scale_fill_manual().

emeanplot2 &lt;- ggplot(emeanframe,
                     aes(x = emean_Sleep_State,
                         y = realmean,
                         fill = emean_Treatment)) +
  geom_col(position = &quot;dodge&quot;,
           color = &quot;black&quot;) +
  geom_errorbar(aes(ymin = realminmean,
                    ymax = realmaxmean),
                color = &quot;black&quot;,
                width = 0.2,
                position = position_dodge(width = 0.9),
                linewidth = 1) +
  scale_x_discrete(limits = c(&quot;WAKE&quot;, &quot;SWS&quot;, &quot;REM&quot;)) +
  labs(x = &quot;Sleep State&quot;,
       y = &quot;Proportion of Behaviour&quot;,
       color = &quot;Type of Night&quot;,
       size = 14) +
  ylim(0, 1) +
  guides(fill = guide_legend(title = &quot;Type of Night&quot;)) +
  theme(axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 12),
        axis.title.x = element_text(size = 14),
        axis.title.y = element_text(size = 14)) +
  scale_fill_manual(values = custom_colors)

huangapple
  • 本文由 发表于 2023年6月6日 17:08:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76413059.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定