生成带有比填充颜色稍深的线条的箱线图

时间:2019-04-15 12:40:33

标签: r ggplot2 rgb

要生成较少分散注意力的箱形图,最好为try { BrokerService bs = mqManager.getBrokerService(); System.out.println(bs.getBroker().getBrokerId()); MessageProducer mp = mqManager.getMessageProducer(mqName); // scrittura del messaggio sulla coda TextMessage txtMsg = mqManager.getTextMessage("Finito"); //System.out.println("Scrivo sulla coda " + mqName); mp.send(txtMsg); } catch ( Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } public static void main(String[] args) throws JMSException, InterruptedException { String url = "vm://127.0.0.1:61618? wireFormat.maxInactivityDurationInitalDelay=30000"; if (args.length > 0) { url = args[0]; } ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(url); Destination destination = new ActiveMQQueue("LEO"); Connection connection = connectionFactory.createConnection(); connection.start(); Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); MessageConsumer consumer = session.createConsumer(destination); for (; ; ) { System.out.println("Waiting for message."); Message message = consumer.receive(); if (message == null) { break; } System.out.println("Got message: " + message); } connection.close(); } 使用相似的色标。 我如何才能做到这一点而无需猜测?

2 个答案:

答案 0 :(得分:4)

预定义的颜色(scale _ * _ manual)

使用colorspace::darken()

的简单解决方案

这是我猜最简单的解决方案:

library(ggplot2)
library(colorspace)

hex_colors = c(
  setosa = "#80adf7", 
  versicolor = "#96ef8f", 
  virginica = "#f4a1df")

g = ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species, color = Species))
g + geom_boxplot() + 
  scale_fill_manual(values = hex_colors) + 
  scale_color_manual(values = darken(hex_colors, amount = 0.3))

手动计算颜色

如果要控制更多,可以将HEX转换为RGB,并稍微降低红色,绿色和蓝色的所有值。 您还可以将其转换为HSV,并通过在移液管中添加rgb2hsv并在最后调用hsv而不是rgb来更轻松地播放色相,饱和度和亮度值。

library(ggplot2)
library(magrittr)
library(purrr)

hex_colors = c(
  setosa = "#80adf7", 
  versicolor = "#96ef8f", 
  virginica = "#f4a1df")

dark_colors = hex_colors %>% 
  col2rgb %>% #convert HEX colors to RGB Matrix
  "*"(0.7) %>% # make each component "darker"
  apply(2, lift_dv(rgb, maxColorValue = 255)) # Convert each column to HEX again

g = ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species, color = Species))
g + geom_boxplot() + 
  scale_fill_manual(values = hex_colors) + 
  scale_color_manual(values = dark_colors)

两种方法均会导致以下结果:

Boxplots

自动颜色(scale _ * _ discrete)

scale_*_discrete适用于HSL颜色空间。我们可以手动定义亮度。

library(ggplot2)
g = ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species, color = Species))
g + geom_boxplot() + 
  scale_fill_discrete(l = 70) + # a bit brighter
  scale_color_discrete(l = 50) # a bit darker

boxplot 2

答案 1 :(得分:2)

另一种解决方案是将alpha()传递给scale_FOO_manual。有了它,我们只需要指定想要的alpha和颜色值:

library(ggplot2)

color <- c("red", "blue", "green")
alpha_color <- 1
alpha_fill <- 0.2

ggplot(iris, aes(Species, Sepal.Length, fill = Species, color = Species)) +
  geom_boxplot() + 
  scale_fill_manual(values = alpha(color, alpha_fill)) + 
  scale_color_manual(values = alpha(color, alpha_color))

enter image description here