用颜色变量用R中的ggplot2绘制数据

时间:2021-12-12 00:05:47

This is something of an extension of a previous question here:

这是前一个问题的扩展:

Assign colors to a data frame based on shared values with a character string in R

根据R中字符串的共享值为数据框指定颜色

I now have a data frame with x, y, errrs, and newcolors and I want to plot the data and error bars by color using ggplot2. I've tried this, and the plot works well, but the colors are not even close to being correct. I've tried defining the color variables in different places within the ggplot() call, but no luck. What am I missing?

我现在有一个带有x,y,errrs和newcolors的数据框,我想用ggplot2按颜色绘制数据和误差线。我试过这个,情节很好,但颜色甚至都不正确。我已经尝试在ggplot()调用中的不同位置定义颜色变量,但没有运气。我错过了什么?

Here is the data:

这是数据:

names          <- c( "TC3", "102", "172", "136", "142", "143", "AC2G" )
colors         <- c( "darkorange", "forestgreen", "darkolivegreen", "darkgreen", "darksalmon", "firebrick3", "firebrick1" )
dataA          <- c( "JR13-101A", "TC3B", "JR12-136C", "AC2GA", "TC3A" )
newcolors      <- rep( NA, length( dataA ) )
dataA          <- as.data.frame( cbind( dataA, newcolors ) )
x              <- c( 1, 2, 3, 4, 5 )
y              <- c( 10, 6, 3, 18, 2 )
errs           <- c( 2, 1, 2, 1, 2 )

dataA          <- cbind( dataA, x, y, errs )

and a solution to my previous question by @Dave2e that assigns colors by sample name:

以及@ Dave2e的前一个问题的解决方案,按样本名称分配颜色:

dataA$newcolors <- as.character( dataA$newcolors )
 for( j in 1:length( names ) ) {
  dataA$newcolors[ grep( names[ j ], dataA$dataA ) ] <- colors[ j ] 
}

and finally the plotting code I've tried:

最后我试过的绘图代码:

ggplot( dataA, aes( x = x, y = y) ) +
  geom_errorbar( aes( ymin = y - errs, ymax = y + errs, color = newcolors ), 
                 width = 0.03 ) + 
  geom_point( size = 5, aes( color = newcolors) ) 

(I've also tried putting the colors into the aes() call up front by aes( x = x, y = y, color = newcolors). The plot looks good, apart from the fact that the colors are not correct. "darkorange" shows up as a light green "darkgreen" is some pinkish color, and "firebrick1" is a light blue.

(我也尝试将颜色放入aes()前面的aes调用(x = x,y = y,color = newcolors)。除了颜色不正确之外,情节看起来还不错。 darkorange“显示为浅绿色”darkgreen“是一些粉红色,而”firebrick1“是浅蓝色。

1 个解决方案

#1


2  

In response to this comment of the OP, you may try

在回应OP的这个评论时,您可以尝试

ggplot(dataA, aes(x = x, y = y, fill = I(newcolors))) +
  geom_errorbar(aes(ymin = y - errs, ymax = y + errs), 
                 width = 0.03) + 
  geom_point(size = 5, shape = 21) 

用颜色变量用R中的ggplot2绘制数据

In geom_point() size, shape and border color have been explicitely defined, i.e., outside of a call to aes(). Shapes numbered 21 to 24 are filled (see http://ggplot2.tidyverse.org/reference/scale_shape.html for available shapes). Consequently, the fill aesthetic but not the color aesthetic has been defined in the call to aes(). So, error bars and symbol borders are printed in black by default.

在geom_point()中,明确定义了形状和边框颜色,即在对aes()的调用之外。填充编号为21到24的形状(有关可用形状,请参阅http://ggplot2.tidyverse.org/reference/scale_shape.html)。因此,在对aes()的调用中已经定义了填充美学而不是颜色美学。因此,默认情况下,错误栏和符号边框以黑色打印。

The advantage (or drawback, perhaps) is that the data point which had been assigned NA as newcolor is visible.

可能的优点(或缺点)是已经将NA指定为新颜色的数据点是可见的。

For comparison, below is how the plot looked after picking up the suggestion from Richard Telford's comment:

为了比较,以下是在理查德特福德的评论中提出建议之后情节的看法:

ggplot(dataA, aes(x = x, y = y, color = I(newcolors))) +
  geom_errorbar(aes(ymin = y - errs, ymax = y + errs), 
                width = 0.03) + 
  geom_point(size = 5) 

用颜色变量用R中的ggplot2绘制数据

Note that the leftmost data point was removed by ggplot2 as there was no color, i.e., NA, assigned to this data point.

注意,最左边的数据点被ggplot2删除,因为没有颜色,即NA,分配给该数据点。

Warning message:
Removed 1 rows containing missing values (geom_point).

警告消息:删除了包含缺失值的1行(geom_point)。

#1


2  

In response to this comment of the OP, you may try

在回应OP的这个评论时,您可以尝试

ggplot(dataA, aes(x = x, y = y, fill = I(newcolors))) +
  geom_errorbar(aes(ymin = y - errs, ymax = y + errs), 
                 width = 0.03) + 
  geom_point(size = 5, shape = 21) 

用颜色变量用R中的ggplot2绘制数据

In geom_point() size, shape and border color have been explicitely defined, i.e., outside of a call to aes(). Shapes numbered 21 to 24 are filled (see http://ggplot2.tidyverse.org/reference/scale_shape.html for available shapes). Consequently, the fill aesthetic but not the color aesthetic has been defined in the call to aes(). So, error bars and symbol borders are printed in black by default.

在geom_point()中,明确定义了形状和边框颜色,即在对aes()的调用之外。填充编号为21到24的形状(有关可用形状,请参阅http://ggplot2.tidyverse.org/reference/scale_shape.html)。因此,在对aes()的调用中已经定义了填充美学而不是颜色美学。因此,默认情况下,错误栏和符号边框以黑色打印。

The advantage (or drawback, perhaps) is that the data point which had been assigned NA as newcolor is visible.

可能的优点(或缺点)是已经将NA指定为新颜色的数据点是可见的。

For comparison, below is how the plot looked after picking up the suggestion from Richard Telford's comment:

为了比较,以下是在理查德特福德的评论中提出建议之后情节的看法:

ggplot(dataA, aes(x = x, y = y, color = I(newcolors))) +
  geom_errorbar(aes(ymin = y - errs, ymax = y + errs), 
                width = 0.03) + 
  geom_point(size = 5) 

用颜色变量用R中的ggplot2绘制数据

Note that the leftmost data point was removed by ggplot2 as there was no color, i.e., NA, assigned to this data point.

注意,最左边的数据点被ggplot2删除,因为没有颜色,即NA,分配给该数据点。

Warning message:
Removed 1 rows containing missing values (geom_point).

警告消息:删除了包含缺失值的1行(geom_point)。