如何使用ggplot2在圆形条形图上添加箭头

问题描述 投票:0回答:1

我想添加一个代表平均角度和大小的箭头(如图https://i.stack.imgur.com/mZ8lB.png)。到目前为止,我们使用“circular”包计算了平均角度(= 56.28)和R值(= 0.6828)。另一方面,使用“ggplot2”和下面的脚本制作了图形。谁能帮忙放这个箭头吗?

提前致谢。

数据:

mes <- c("janeiro", "fevereiro", "marco", "abril", "maio", "junho", "julho", "agosto", "setembro", "outubro", "novembro", "dezembro")
flor <- c(66.67, 93.33, 95.00, 93.33, 65.00, 20.00, NA, NA,  0.00, 0.00, 0.00, 40.00)
angulo <- c(0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330)
dados_canavalia <- data.frame (mes, angulo, flor)
dados_canavalia$mes<- factor(dados_canavalia$mes, ordered = TRUE, levels =c("janeiro","fevereiro", "marco", "abril", "maio", "junho", "julho", "agosto", "setembro", "outubro", "novembro", "dezembro"))
str(dados_canavalia)  

脚本:

plt_canavalia <- ggplot(dados_canavalia) +   # Your data name here
  # Make custom panel grid
  geom_hline(
    aes(yintercept = y), 
    data.frame(y = c(0,25,50,75,100)),#Your custom data range
    color = "lightgrey"
    ) +
  geom_col(
    aes(
      x = mes, # The x axis to reorder from minimum to maximum
      y = flor, # This axis represents the height of the bars in barplot.
      fill = flor # The represents the colour intensity of the barplot.
    ),
    position = "dodge2",
    show.legend = FALSE,
    alpha = .9
  )+
  geom_segment(
    aes(
      x = mes,
      y = 0,
      xend = mes,
      yend = 100 #The maximum values you want to extend the line.
    ),
    linetype = "dashed",
    color = "gray12"
  ) + 
  coord_polar()+  # Make it circular!
  scale_y_continuous(
    limits = c(0, 100),
    expand = c(0,0),
    breaks = c(0,50,100)
  ) + 
  scale_fill_gradientn(               # New fill and legend title for number of tracks per region
    "Porcentagem de indivíduos (%)",
    colours = c( "#F8D8D8","#F8A8A8","#F07070","#E85830"),
    limits= c(0,100),
    breaks = seq(0, 100, by = 25)
  ) +
  # Make the guide for the fill discrete
  guides(
    fill = guide_colorsteps(
      barwidth = 35, barheight = 1, title.position = "top", title.hjust = .5
    )
  ) +
  theme(
    # Remove axis ticks and text
    axis.title = element_blank(),
    axis.ticks = element_blank(),
    axis.text.y = element_blank(),
    # Use gray text for the region names
    axis.text.x = element_text(color = "gray12", size = 30), #TAMANHO das letras dos meses
    # Move the legend to the bottom
    legend.position = "bottom"
  )+
  # Add labels
  labs(
    title = "\nCanavalia rosea",
    sep = "\n") +
  # Customize general theme
  theme(
    text = element_text(color = "gray12", family = "Arial"), # Set default color and font family for the text
    plot.title = element_text(face = "italic", size = 35 , hjust = 0.04),   # Customize the text in the title, subtitle, and caption
    legend.text = element_text(size = 27),
    legend.title = element_text(size = 24),
    panel.background = element_rect(fill = "white", color = "white"),  # Make the background white and remove extra grid lines
    panel.grid = element_blank(),
    panel.grid.major.x = element_blank())+
  scale_x_discrete(labels = c("Jan","Fev", "Mar", "Abr", "Mai", "Jun", "Jul", "Ago", "Set", "Out", "Nov", "Dez"))
  
 

r ggplot2 plot
1个回答
0
投票

这一层应该可以做到。

我们可以使用

annotate
添加一个与全局源数据框(或任何其他数据框)无关的一次性几何对象。我们在这里转换 x 值,因为您的值是基于月份的。您的 Fev 是第二个轴步长,但与角度 60 相关,因此我们可以将角度除以 30 以获得右轴步长。

  annotate("segment", x = 56.28/30, xend = 56.28/30,
           y = 0, yend = 68.28, 
           linewidth = 1, 
           arrow = arrow(angle = 20, type = "closed", length = unit(0.5, "line"))) +

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.