在分配频率上标注平均值

时间:2019-01-22 16:42:05

标签: r ggplot2

我想绘制一个总频率分布图并注释平均值。最直接的方法是:

from jira.client import JIRA
import json
options = {
'server': 'https://some.server.net/jira12/projects/XXXX',
 }
USERNAME=input("Enter your username: ")
PASSWORD=input("Enter your password: ")
jira = JIRA(options, basic_auth=(USERNAME, PASSWORD))
issue = jira.issue('XXXX-260', expand='changelog')

enter image description here

问题显示出何时我要将平均值准确地放在平均值线的中间:

library(dplyr)
library(ggplot2)
load(mtcars)
mtcars0=mtcars%>%group_by(cyl)%>%mutate(MeanMpg=round(mean(mpg),2))
mtcars1=mtcars%>%group_by(cyl)%>%summarize(MeanMpg=round(mean(mpg),2))
p <- ggplot(mtcars0, aes(mpg, fill=cyl)) +
  facet_wrap(. ~ cyl) + 
  geom_density(alpha=.2) +
  geom_vline(data=mtcars1, aes(xintercept=MeanMpg), linetype="dashed", size=1) +
  annotate("text", label = labels, size = 4, x = 15, y = 0.26)
p 

enter image description here

在这种情况下,R将文本乘以,我得到一个错误。     我如何才能将标签放置在构面的每个平均位置上?

1 个答案:

答案 0 :(得分:1)

您可以使用mtcars1数据指定标签的位置:

ggplot(mtcars0, aes(mpg, fill=cyl)) +
  facet_wrap(. ~ cyl) + 
  geom_density(alpha=.2) +
  geom_vline(data = mtcars1, aes(xintercept = MeanMpg), linetype="dashed", size=1) +
  geom_text(data = mtcars1, aes(x = MeanMpg, y = 0.25, label = MeanMpg))

enter image description here