简体   繁体   English

如何定位条 plot label 在固定 position 的 Z32FA6E1B78A9D4028953E605Z46 区域

[英]How to locate bar plot label in a fixed position of the plot area in ggplot

I am working in bar race plot where only one bar in each plot will have a label.我在酒吧比赛 plot 中工作,每个 plot 中只有一个酒吧会有 label。 I want that label to appear in the same position in the bar race plot regardless of the data and ranking in each year (for example, I would like to place the label at the bottom right side of the chart). I want that label to appear in the same position in the bar race plot regardless of the data and ranking in each year (for example, I would like to place the label at the bottom right side of the chart). In other words, I would like to locate the label in the same coordinates (x,y) in the chart.换句话说,我想将 label 定位在图表中的相同坐标 (x,y) 中。 The following code is an example of what I can do right now.下面的代码是我现在可以做的一个例子。

library(ggimage)
library(janitor)
library(gganimate)
library(ggplot2)
library(tidyverse)
library(transformr)
library(ggforce)
theme_set(theme_classic())
data_bar <- structure(list(firm = c("audi", "ford", "mercedes", "toyota", 
"audi", "ford", "mercedes", "toyota", "audi", "ford", "mercedes", 
"toyota", "audi", "ford", "mercedes", "toyota", "audi", "ford", 
"mercedes", "toyota", "audi", "ford", "mercedes", "toyota", "audi", 
"ford", "mercedes", "toyota", "audi", "ford", "mercedes", "toyota", 
"audi", "ford", "mercedes", "toyota", "audi", "ford", "mercedes", 
"toyota", "audi", "ford", "mercedes", "toyota"), year = c(1945, 
1945, 1945, 1945, 1946, 1946, 1946, 1946, 1947, 1947, 1947, 1947, 
1948, 1948, 1948, 1948, 1949, 1949, 1949, 1949, 1950, 1950, 1950, 
1950, 1951, 1951, 1951, 1951, 1952, 1952, 1952, 1952, 1953, 1953, 
1953, 1953, 1954, 1954, 1954, 1954, 1955, 1955, 1955, 1955), 
    value = c(1156L, 2750L, 48436L, NA, 1170L, 3118L, 48653L, 
    NA, 1189L, 3453L, 47990L, NA, 1240L, 4406L, 49284L, NA, 1245L, 
    5249L, 51490L, NA, 1156L, 5907L, 48436L, 2007L, 1170L, 6311L, 
    48653L, 2042L, 1189L, 6323L, 47990L, 2077L, 1240L, 6594L, 
    49284L, 2110L, 1245L, 7261L, 51490L, 2146L, 1246L, 8054L, 
    49632L, 2179L), rank = c(3, 2, 1, 4, 3, 2, 1, 4, 3, 2, 1, 
    4, 3, 2, 1, 4, 3, 2, 1, 4, 4, 2, 1, 3, 4, 2, 1, 3, 4, 2, 
    1, 3, 4, 2, 1, 3, 4, 2, 1, 3, 4, 2, 1, 3), Value_rel = c(0.0238665455446362, 
    0.0567759517714097, 1, NA, 0.0240478490535013, 0.0640864900417241, 
    1, NA, 0.0247759949989581, 0.0719524901021046, 1, NA, 0.0251602954305657, 
    0.0894002110218326, 1, NA, 0.024179452320839, 0.101942124684405, 
    1, NA, 0.0238665455446362, 0.121954744404988, 1, 0.041436121892807, 
    0.0240478490535013, 0.129714508868929, 1, 0.0419706903993587, 
    0.0247759949989581, 0.131756615961659, 1, 0.0432798499687435, 
    0.0251602954305657, 0.133795958120282, 1, 0.0428130833536239, 
    0.024179452320839, 0.141017673334628, 1, 0.0416779957273257, 
    0.0251047711154094, 0.162274339136041, 1, 0.0439031270148291
    ), Value_lbl = c(" 0", " 0", " 0", " NA", " 0", " 0", " 0", 
    " NA", " 0", " 0", " 0", " NA", " 0", " 0", " 0", " NA", 
    " 0", " 0", " 0", " NA", " 0", " 0", " 0", " 0", " 0", " 0", 
    " 0", " 0", " 0", " 0", " 0", " 0", " 0", " 0", " 0", " 0", 
    " 0", " 0", " 0", " 0", " 0", " 0", " 0", " 0")), row.names = c(NA, 
-44L), class = c("tbl_df", "tbl", "data.frame"))

  staticplot = ggplot(data_bar, aes(rank, group = firm, fill = as.factor(if_else(firm == "ford","darkblue", "darkred")), color = as.factor(if_else(firm == "ford","darkblue", "darkred")))) +
    geom_tile(aes(y = value/2,
                  height = value,
                  width = 0.9), alpha = 0.8, color = NA) +
    geom_text(aes(y = value, label = as.factor(ifelse(firm == "ford", round(value,2),NA))), vjust= 0.3, hjust = 1, size = 20, color = "red") +
    coord_flip(clip = "off", expand = FALSE) +
    scale_y_continuous(labels = scales::comma) +
    scale_x_reverse() +
    guides(color = "none", fill = "none") +
    theme(axis.line=element_blank(),
          axis.text.x=element_text(size = 28),
          axis.text.y=element_blank(),
          axis.ticks=element_blank(),
          axis.title.x=element_blank(),
          axis.title.y=element_blank(),
          legend.position="none",
          panel.background=element_blank(),
          panel.border=element_blank(),
          panel.grid.major=element_blank(),
          panel.grid.minor=element_blank(),
          panel.grid.major.x = element_line( size=.1, color="black" ),
          panel.grid.minor.x = element_line( size=.1, color="black" ),
          plot.title=element_blank(),
          plot.subtitle=element_blank(),
          plot.caption =element_blank(),
          plot.background=element_blank(),
          plot.tag = element_text(size=102, face="italic", color="red"),
          plot.tag.position = c(0.893,0.2),
          plot.margin = margin(2,2, 2, 4, "cm"))
  
  
  anim = staticplot + transition_states(year, transition_length = 1, state_length = 1, wrap = FALSE)
  
  animate(anim, 200, fps = 10, duration = 30, width = 1600, height = 900,
          renderer = ffmpeg_renderer()) -> for_mp4
  
  anim_save("firm position by year.mp4", animation = for_mp4)

By "playing" with hjust and vjust in geom_text(), I can easily move the label (the number in the chart), but always with respect to the bar corresponding to each year.通过在 geom_text() 中“玩” hjust 和 vjust,我可以轻松移动 label(图表中的数字),但始终相对于每年对应的条形。 What I want is to place the number in a fixed position with respect to the plotting area.我想要的是将数字放在相对于绘图区域的固定 position 中。 Therefore, regardless of the data in each year, I will have the label in the same position of the plotting area.因此,无论每年的数据如何,我都会在绘图区的同一position中拥有label。

Thanks in advance提前致谢

Try moving the y out of aes() in geom_text and give it a specific value.尝试将y移出geom_text中的aes()并给它一个特定的值。

df <- data.frame(dose = c("D0.5", "D1", "D2"), len = c(4.2, 10, 29.5))

p <- ggplot(data = df, aes(x = dose, y = len)) +
  geom_bar(stat = "identity")

p + geom_text(aes(label = as.factor(ifelse(dose == "D1", len, NA))),
              x = 2,
              y = 15,
              vjust = 0.3, hjust = 1, size = 20)

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

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