使用facet_wrap时无法向geom_text添加条件

时间:2019-11-24 03:17:00

标签: r ggplot2 geom-bar facet-wrap geom-text

我正在尝试使用ggplot2在r中创建图形,我使用facet_wrap复制3套不同类型设置的堆叠条形图。但是,当我使用geom_text仅标记大于1.5的值时,收到此错误:

Error in `$<-.data.frame`(`*tmp*`, "PANEL", value = c(1L, 1L, 1L, 1L,  : 
  replacement has 24 rows, data has 16

请告知

这是我的图形代码:

ggplot(data, aes(x = year, y = percent, fill = facility,label = round(percent))) + geom_bar(aes(y = percent, x = year, fill = facility), 
           data = data,stat="identity") +
  geom_line(aes(x=year, y =percent),linetype="dashed",color="black", position = 'stack',data = data)+
  geom_text(data = subset(data,percent>1.5),size = 3, position = position_stack(vjust = 0.5),color="white")+
  theme(legend.position="bottom", legend.direction="horizontal",
        legend.title = element_blank()) +
  scale_y_continuous(labels = dollar_format(suffix = "%", prefix = "")) +
  scale_x_continuous(breaks=0:1,
                     labels=c("2010","2015"))+
  labs(x="", y="Percentage") +
  ggtitle("Year comparison") +
  coord_flip()+
  theme(plot.title = element_text(size=20,face="bold",hjust = 0.5))+
  facet_wrap(~data$type, ncol = 1, scales = "free")

Here is the image... I'm basically trying to get rid of the label 0 in the picture

谢谢!

1 个答案:

答案 0 :(得分:1)

在执行resource "aws_instance" "crawl_worker" { ami = "${var.ami_id}" instance_type = "t3a.nano" security_groups = ["${var.aws_security_group}"] key_name = "secret" count = 10 tags = { Name = "crawl_worker_${count.index}" } connection { type = "ssh" host = "${self.public_ip}" user = "ubuntu" private_key = "${file("~/secret.pem")}" timeout = "50m" } provisioner "remote-exec" { inline = [ "python3 aws_crawl.py" ] } } 时,您将包括原始数据帧中“类型”(facet_wrap())的整列,大概是24行。您想使用data$type之类的格式来允许facet_wrap(~ type)使用动态数据到方面。

有点难以解释,但也许很容易在示例数据中试用。

不起作用

ggplot2

会工作

library(tidyverse)
iris %>%
  ggplot(aes(Sepal.Length, Sepal.Width, label = Species)) +
  geom_point() +
  geom_text(data = subset(iris, Sepal.Length > 6)) +
  facet_wrap(~ iris$Species)
#> Error in `$<-.data.frame`(`*tmp*`, "PANEL", value = structure(c(1L, 1L, : replacement has 150 rows, data has 61