在ggplot2

时间:2018-05-11 05:58:00

标签: r ggplot2 geom-bar

我想将自定义文本添加到我的ggplot中的预定义位置 我在各个地方搜索过,我觉得我差不多了,但是我的代码中的某些内容并没有让它正常工作。
以下是示例数据:

x <- data.frame(Snapshot_Date = c(
  "2016-12-31",
  "2016-12-31",
  "2016-12-31",
  "2016-12-31",
  "2017-12-31",
  "2017-12-31",
  "2017-12-31",
  "2017-12-31"),
           NewExisting = c('Existing',
                      'Existing',
                      'New',
                      'New',
                      'Existing',
                      'Existing',
                      'New',
                      'New'
                      ),
           Product = c( 'CC',
                        'ETA',
                        'CC',
                        'ETA',
                        'CC',
                        'ETA',
                        'CC',
                        'ETA'
                      ),
           prop = c(0.2384310,
                    0.4268317,
                    0.2099592,
                    0.4991919,
                    0.2201962,
                    0.4729444,
                    0.1375661,
                    0.5504558)
    ,stringsAsFactors = F)
x$Snapshot_Date <- as.factor(x$Snapshot_Date)
x$NewExisting <- as.factor(x$NewExisting)

我根据不同网站的建议为corrdinates创建了一个单独的df

facet_group <- data.frame(NewExisting = c("Existing","New")) # order matters
lable.coord <- data.frame(
  x=c(-Inf,-Inf),y = c(Inf,Inf),
  facet_group,
  labs = c("lab1","label2"),
  hjust1 = c(0,0),
  vjust1 = c(1,1))

现在,当我绘制图表时,出现错误:Error in FUN(X[[i]], ...) : object 'Snapshot_Date' not found

ggplot(x,aes(x = Product, y = prop, fill = Snapshot_Date)) +
  geom_col(position = position_dodge()) +
  facet_wrap(~NewExisting, ncol = 1, scales = "free") +
  geom_text(aes(label = percent(prop)),
            position = position_dodge(width = 0.9),
            vjust = -.5,
            cex = 4,
            colour = "black"
  )+ # works upto here
  geom_text(data = lable.coord, aes(
    x = x, y = y,
    label = labs,
    group = NULL,
    vjust = vjust1,
    hjust = hjust1
  )) 

在第二次使用geom_text()之前,代码似乎正常工作。似乎使用不同的数据集导致了问题。

如果我调整代码并将fill = Snapshot_date移至geom_col(),则图表会显示。但现在它似乎忽略了position = position_dodge(width = 0.9)参数。

ggplot(x,aes(x = Product, y = prop)) +
  geom_col(aes( fill = Snapshot_Date),position = position_dodge()) +
  facet_wrap(~NewExisting, ncol = 1, scales = "free") +
  geom_text(aes(label = percent(prop)),
            position = position_dodge(width = 0.9),
            vjust = -.5,
            cex = 4,
            colour = "black"
  )+
  geom_text(data = lable.coord, aes(
    x = x, y = y,
    label = labs,
    group = NULL,
    vjust = vjust1,
    hjust = hjust1
  )) 

enter image description here

如何将自定义文字保留在角落中并让它在条形图上显示标签?

谢谢

2 个答案:

答案 0 :(得分:1)

group=Snapshot_date geom_text中添加aes

ggplot(x,aes(x = Product, y = prop)) +
  geom_col(aes( fill = Snapshot_Date),position = position_dodge(.9)) +
  facet_wrap(~NewExisting, ncol = 1, scales = "free") +
  geom_text(aes(label = prop, group=Snapshot_Date),
            position = position_dodge(width = 0.9),
            vjust = -.5,
            cex = 4,
            colour = "black"
  ) +
  geom_text(data = lable.coord, aes(
    x = x, y = y,
    label = labs,
    group = NULL,
    vjust = vjust1,
    hjust = hjust1
  )) 

enter image description here

答案 1 :(得分:0)

我意识到Snapshot_Date label.coorddf遗漏了什么。您需要确保拥有图表中使用的df中的所有因素 当它给我一个错误时:Error in FUN(X[[i]], ...) : object 'Snapshot_Date' not found
我以为它是在谈论源数据x df。但实际上它指的是label.coord df

这是最终的工作代码:

facet_group <- data.frame(NewExisting = c("Existing","New"), Snapshot_Date = c('2016-12-31','2017-12-31')) 
lable.coord <- data.frame(
  x=c(-Inf,-Inf),y = c(Inf,Inf),
  facet_group,
  labs = c("lab1","label2"),
  hjust1 = c(0,0),
  vjust1 = c(1,1))


ggplot(x,aes(x = Product, y = prop,fill = Snapshot_Date)) +
  geom_col(position = position_dodge()) +
  facet_wrap(~NewExisting, ncol = 1, scales = "free") +
  geom_text(aes(label = percent(prop)),
            position = position_dodge(width = 0.9),
            vjust = -.5,
            cex = 4,
            colour = "black"
  )+
  geom_text(data = lable.coord, aes(
    x = x, y = y,
    label = labs,
    group = NULL,
    vjust = vjust1,
    hjust = hjust1
  ),position = position_dodge(width = 0.9)) 

enter image description here