简体   繁体   English

如何在geom_jitter中使用其抖动的相应颜色为xlabs着色?

[英]How to colour xlabs with the corresponding colour of its jitter in a geom_jitter?

I am trying to colour the xlabs with the same colour as the point they are labelling, but I am having some trouble.我试图用与它们标记的点相同的颜色为 xlab 着色,但我遇到了一些麻烦。

Each jitter is coloured depending on a specified variable levels, and I want the same for the xlabs.每个抖动都根据指定的变量级别着色,我希望 xlabs 也是如此。

This is my code to plot the figure:这是我绘制图形的代码:

ggplot(coverage_data, aes(x=x_values, y=coverage_data$mean, fill=coverage_data$frecuency))+
  geom_jitter(size=2.5, shape=21, stroke=1.5)+
  scale_fill_manual(name = "frecuency", values =c("deepskyblue4", "gray67", "darkgoldenrod2", "springgreen4", "brown1", "white"))+
  xlab("Id")+
  ylab("max coverage")+
  theme(axis.text.x=element_text(hjust=1, colour = 'black', size = 9))

If I declare colour ( in theme(axis.text.x(element_text)) ) as a vector I get an error.如果我将颜色( in theme(axis.text.x(element_text)) )声明为向量,则会出现错误。 Do you know how can I achieve that?你知道我怎样才能做到这一点吗?

Passing a vector of colors generates a warning, but with ggplot2 3.3.0 (what I'm running) it does work.传递颜色向量会产生警告,但使用ggplot2 3.3.0 (我正在运行的)它确实有效。

Since you didn't share any data I've made some up:由于您没有共享任何数据,因此我编了一些:

frecuency <- rep(c("A", "B", "C", "D", "E", "F"), 10)
mean <- runif(60, 10, 20)
x_values <- runif(60, 1, 100)

coverage_data <- data.frame(frecuency, mean, x_values, stringsAsFactors = FALSE)

ggplot(coverage_data, aes(x= x_values, y= mean, fill= frecuency))+
  geom_jitter(size=2.5, shape=21, stroke=1.5)+
  scale_fill_manual(name = "frecuency", values =c("deepskyblue4", "gray67", "darkgoldenrod2", "springgreen4", "brown1", "white"))+
  xlab("Id")+
  ylab("max coverage")+
  theme(axis.text.x=element_text(hjust=1, colour = c("black", "blue", "green", "yellow", "red"), size = 9))

Warning message: Vectorized input to element_text() is not officially supported.警告消息:对element_text()矢量化输入不受官方支持。 Results may be unexpected or may change in future versions of ggplot2.结果可能出乎意料,也可能在 ggplot2 的未来版本中发生变化。

在此处输入图片说明

sessionInfo()
R version 3.6.2 (2019-12-12)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)

other attached packages:
[1] ggQC_0.0.31     readxl_1.3.1    forcats_0.5.0  
[4] stringr_1.4.0   dplyr_0.8.3     purrr_0.3.3    
[7] readr_1.3.1     tidyr_1.0.2     tibble_2.1.3   
[10] ggplot2_3.3.0   tidyverse_1.3.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM