更改颜色并在ggplot2 geom_freqpoly上添加形状

时间:2021-03-30 23:46:14

I'm trying to add shapes on the lines plotted using geom_freqpoly to give more visibility to them if the plot is printed b/w on paper.

我正在尝试在使用geom_freqpoly绘制的线条上添加形状,以便在纸张上以黑白打印图形时为它们提供更多可见性。

data <-  data.frame(time=runif(1000,0,20000),
                   class=c("a","b","c","d"))

ggplot(data, aes(time, colour = class)) + geom_freqpoly(binwidth = 1000) + geom_point(aes(shape=class))

but this generates this error: 'Error: geom_point requires the following missing aesthetics: y'

但是这会产生这个错误:'错误:geom_point需要以下缺少美学:y'

How can I solve this error?

我该如何解决这个错误?

Another thing is that I want to use a single colour (eg. blue) to draw the lines but with scale_colour_brewer() I can't change the colour scale, I want to change it because the lightest colour is nearly white and you can barely see it.

另一件事是我想使用单一颜色(例如蓝色)绘制线但是使用scale_colour_brewer()我不能改变颜色比例,我想改变它,因为最浅的颜色几乎是白色而你几乎不能看见。

How can I add a custom min and max for the colours?

如何为颜色添加自定义最小值和最大值?

更改颜色并在ggplot2 geom_freqpoly上添加形状

1 个解决方案

#1


1  

How about this? The error you are getting is being produced by geom_point which needs x and y, so I removed it.

这个怎么样?你得到的错误是由需要x和y的geom_point产生的,所以我删除了它。

ggplot(data, aes(x = time, color = class)) + 
  geom_freqpoly(binwidth = 1000) +
  scale_color_brewer(palette = "Blues") +
  theme_dark()

更改颜色并在ggplot2 geom_freqpoly上添加形状

If you don't want the dark background, pass manual values from RColorBrewer. The following example uses every second color to increase the contrast.

如果您不想要深色背景,请从RColorBrewer传递手动值。以下示例使用每隔一种颜色来增加对比度。

p1 <- ggplot(data, aes(x = time, color = class)) + 
  geom_freqpoly(binwidth = 1000) +
  scale_color_manual(values = RColorBrewer::brewer.pal(9, name = "Blues")[c(3, 5, 7, 9)])

EDIT

编辑

You can extract summarised data from a ggplot object using layer_data function.

您可以使用layer_data函数从ggplot对象中提取汇总数据。

xy <- layer_data(p1)

ggplot(xy, aes(x = x, y = count, color = colour)) +
  theme_bw() +
  geom_line() +
  geom_point() +
  scale_color_manual(values = RColorBrewer::brewer.pal(9, name = "Blues")[c(3, 5, 7, 9)])

#1


1  

How about this? The error you are getting is being produced by geom_point which needs x and y, so I removed it.

这个怎么样?你得到的错误是由需要x和y的geom_point产生的,所以我删除了它。

ggplot(data, aes(x = time, color = class)) + 
  geom_freqpoly(binwidth = 1000) +
  scale_color_brewer(palette = "Blues") +
  theme_dark()

更改颜色并在ggplot2 geom_freqpoly上添加形状

If you don't want the dark background, pass manual values from RColorBrewer. The following example uses every second color to increase the contrast.

如果您不想要深色背景,请从RColorBrewer传递手动值。以下示例使用每隔一种颜色来增加对比度。

p1 <- ggplot(data, aes(x = time, color = class)) + 
  geom_freqpoly(binwidth = 1000) +
  scale_color_manual(values = RColorBrewer::brewer.pal(9, name = "Blues")[c(3, 5, 7, 9)])

EDIT

编辑

You can extract summarised data from a ggplot object using layer_data function.

您可以使用layer_data函数从ggplot对象中提取汇总数据。

xy <- layer_data(p1)

ggplot(xy, aes(x = x, y = count, color = colour)) +
  theme_bw() +
  geom_line() +
  geom_point() +
  scale_color_manual(values = RColorBrewer::brewer.pal(9, name = "Blues")[c(3, 5, 7, 9)])