简体   繁体   English

ggplot2 - 将平均值添加到我的置信区间

[英]ggplot2 - adding the mean to my Confidence Interval

I am new to ggplot2 - until now I always used the R base plot functions.我是 ggplot2 的新手——直到现在我一直使用 R 基础 plot 函数。 Anyway, I have some data that I was able to plot, but I'm not quite satisfied with the result yet.无论如何,我有一些数据可以 plot,但我对结果还不太满意。 Here's some code to reproduce the plot:这是一些重现 plot 的代码:

library( ggplot2)
library( ggstance)
method <- rep( c( 'a', 'b', 'c', 'd', 'e', 'f'), 6)        
scheme <- c( rep( 'sc1', 18), rep( 'sc2', 18))
classif <- runif( 36, min = 0.4, max = 0.6)
para <- c( rep( 0.1, 6), rep( 0.2, 6), rep( 0.3, 6), rep( 0.4, 6), rep( 0.5, 6), rep( 0.6, 6))
five <- runif( 36, max = .3)
ninetyfive <- runif( 36, min = .7)
avg <- ninetyfive - five
dat <- data.frame( method, scheme, classif, para, five, ninetyfive, avg)
dat[ c( 'para', 'method', 'scheme')] <- lapply( dat[ c( 'para', 'method', 'scheme')], FUN = as.factor)
ggplot( dat, aes( x = classif, y = method)) + 
  labs( x = 'Number of features', y = 'method') + 
  geom_point() + 
  geom_errorbarh( aes( xmin = five, xmax = ninetyfive)) + 
  facet_wrap( vars( para, scheme))

The result looks like this:结果如下所示:

在此处输入图像描述

I want to amend two things: First, and most important, I want to add the avg column to my plot with a different symbol/colour.我想修改两件事:首先,也是最重要的,我想用不同的符号/颜色将avg列添加到我的 plot 中。 Second, I'd love to amend the text in each of those boxes ( the ones that contain 0.1, 0.2, ..., 0.6) and I would also like to change the text sc1 and sc2 to something different - but I don't want to change it in the data-frame.其次,我想修改每个框中的文本(包含 0.1、0.2、...、0.6 的文本),我还想将文本 sc1 和 sc2 更改为不同的内容 - 但我没有不想在数据框中更改它。 Any ideas?有任何想法吗?

To change symbol/color of any geom in ggplot, you modify its aesthetics.要更改 ggplot 中任何几何图形的符号/颜色,请修改其美学。 In general, ggplot's reference site is a great source for information about ggplot.一般来说, ggplot 的参考站点是有关 ggplot 信息的重要来源。 In your case, you simply modify add another geom_point() with avg , and modify its aesthetics as follows:在您的情况下,您只需修改添加另一个geom_point()avg ,并修改其美学如下:

ggplot( dat, aes( x = classif, y = method)) + 
  labs( x = 'Number of features', y = 'method') + 
  geom_errorbarh( aes( xmin = five, xmax = ninetyfive)) + 
  geom_point(aes(x = classif,y = method)) + 
  geom_point(aes(x = avg,y = method), shape = 2, color = "red") +
  facet_wrap( vars( para, scheme))

You can change shape/color/alpha/etc by checking out the aesthetic options for geom_point .您可以通过查看geom_point的美学选项来更改形状/颜色/alpha/等。

As for changing the labels of facet_wrap without changing the data-frame, check out this particular answer of a previous question: https://stackoverflow.com/a/12104207/9985527 .至于在不更改数据帧的情况下更改facet_wrap的标签,请查看上一个问题的特定答案: https://stackoverflow.com/a/12104207/9985527

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

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