ggplot2如何在两个小节之间插入差异

时间:2019-06-22 22:19:50

标签: r ggplot2 graph

我执行了以下代码:

    ggplot(data = df, aes(x = sex,y = weight, fill=sex))+
  geom_bar(position="dodge", stat="summary",width = 0.3) +theme_classic() +
  scale_y_continuous(breaks = round(seq(min(0), max(400), by = 50), digits=2),limits=c(0,400)) +
  labs(x = element_blank(),y="Weight (kg)") + 
  labs( title= "Weight") + 
  scale_fill_manual(values = c("red", "black")) +
  theme (plot.title=element_text( hjust=0.5, vjust=0.5, face='bold'),axis.text.x=element_text(size = 10),axis.text.y=element_text(size = 9),
         axis.title.y = element_text(size = 12),legend.position = "none",
         axis.title.x = element_text(size = 12))

我得到了这个情节:

enter image description here

但是我想插入这两个小节之间的区别,就像这样或类似:

enter image description here

我使用了这个数据集:

set.seed(1234)
top <- data.frame(
  sex=factor(rep(c("Male","Female"), each=200)),
  weight=round(c(rnorm(200, mean=350, sd=5),
                 rnorm(200, mean=300, sd=5)))
)

1 个答案:

答案 0 :(得分:1)

这不是您想要的,但这是一个选择。我们可以使用class BindableObjectPublisher<PublisherType: Publisher>: BindableObject where PublisherType.Failure == Never { typealias Data = PublisherType.Output var didChange: PublisherType var data: Data? init(didChange: PublisherType) { self.didChange = didChange _ = didChange.sink { (value) in self.data = value } } } extension Publisher where Failure == Never { func bindableObject() -> BindableObjectPublisher<Self> { return BindableObjectPublisher(didChange: self) } } struct ContentView : View { @ObjectBinding var binding = Publishers.Just("test").bindableObject() var body: some View { Text(binding.data ?? "Empty") } } 计算均值,找到两个值之间的差,然后将其作为aggregate标签放在具有较低geom_text的新geom_bar上方。

alpha

graph