如何使用ggplot2在R中获取风玫瑰图的Angular 标签?

ie3xauqp  于 2023-06-27  发布在  Angular
关注(0)|答案(1)|浏览(126)

我想绘制一个单独的传说,我的风玫瑰图,其中的标签是根据18个类别的阴谋Angular 。ggplot可以做到吗?
输出的示例:(https://i.stack.imgur.com/hJOf0.jpg
下面的示例代码给我的标签是水平的(也扩展到图的外面)。(https://i.stack.imgur.com/I31LY.jpg

library(ggplot2)
library(RColorBrewer)
library(dplyr)

nb.cols <- 18
mycolors <- colorRampPalette(brewer.pal(8, "Set2"))(nb.cols)

df <- data.frame(x=1, y=1:18)
NCP <- c("Maintenance of options", "Food and feed", "Materials, companionship and labor", "Medicinaland genetic resources", "Energy",                                  
"Physical and psychological experiences",  "Supporting identities", "Learning and inspiration", "Formation of soils and sediments",        
"Regulation of hazards and extreme events", "Regulation of freshwater quantity", "Regulation of freshwater quality", "Regulation of climate",                   
"Habitat creation and maintenance", "Regulation of biological processes", "Pollination and dispersal of seeds",      
"Regulation of air quality", "Regulation of ocean acidification")    
df <- cbind(df, as.data.frame(NCP))

#plot for individual region
ggplot(df,
       aes(x = NCP, y = y)) +
  geom_col(width = 1, fill = mycolors) +
  coord_polar(start = 0) + # change start value if you want a different orientation
  theme_void() +
  theme(axis.title = element_blank(),
        panel.ontop = FALSE, # change to FALSE for grid lines below the wind rose
        panel.background = element_blank())

#Plot for labels
ggplot(df,
       aes(x = NCP, y = x)) +
  geom_col(width = 1, fill = mycolors) +
  coord_polar(start = 0) + # change start value if you want a different orientation
  theme(panel.grid.major = element_blank(), 
        axis.line = element_blank(), 
        plot.background = element_blank(),
        panel.background = element_blank(), 
        axis.title = element_blank(), 
        axis.ticks.y = element_blank(),
        axis.text.y=element_blank(),
        axis.text.x = element_text(colour = "Black", size= 6),
        panel.ontop = FALSE)

我试过调整不同的axis.text.x元素,但我似乎不能得到它的Angular 。

mfuanj7w

mfuanj7w1#

也许有一个更简单的方法,但一个选择是使用geomtextpath::geom_textsegment在条形图的顶部添加标签:

library(ggplot2)
library(RColorBrewer)
library(geomtextpath)

nb.cols <- 18
mycolors <- colorRampPalette(brewer.pal(8, "Set2"))(nb.cols)

df <- data.frame(x = 1, y = 1:18)
NCP <- c(
  "Maintenance of options", "Food and feed", "Materials, companionship and labor", "Medicinaland genetic resources", "Energy",
  "Physical and psychological experiences", "Supporting identities", "Learning and inspiration", "Formation of soils and sediments",
  "Regulation of hazards and extreme events", "Regulation of freshwater quantity", "Regulation of freshwater quality", "Regulation of climate",
  "Habitat creation and maintenance", "Regulation of biological processes", "Pollination and dispersal of seeds",
  "Regulation of air quality", "Regulation of ocean acidification"
)
df <- cbind(df, as.data.frame(NCP))
mycolors <- setNames(mycolors, NCP)

ggplot(
  df,
  aes(x = NCP, y = x)
) +
  geom_textsegment(aes(label = NCP, xend = NCP, y = 1.05, yend = Inf),
    hjust = 0, color = NA, textcolour = "black", size = 6 / .pt
  ) +
  geom_col(aes(fill = NCP), width = 1) +
  scale_fill_manual(values = mycolors, guide = "none") +
  theme(
    panel.grid.major = element_blank(),
    axis.line = element_blank(),
    plot.background = element_blank(),
    panel.background = element_blank(),
    axis.title = element_blank(),
    axis.ticks.y = element_blank(),
    axis.text = element_blank(),
    panel.ontop = FALSE
  ) +
  ylim(c(0, 1.5)) +
  coord_polar(start = 0)

相关问题