简体   繁体   中英

How can I reasonably add a scale and title to circular bar plot?

EDITED: to make a reproducible example

Code:

a <- c(1,2,3,4,5,6,7,8,9,10)
b <- c(65,12,72,58,69,42,32,77,90,10)
argh <- data.frame(a,b)
argh

library(tidyverse)

label_data <- argh

number_of_bar <- nrow(label_data)
angle <-  90 - 360 * (label_data$a-0.5) /number_of_bar  

label_data$hjust<-ifelse( angle < -90, 1, 0)


ggplot(argh, aes(x=a, y=b)) + 
  geom_bar(stat="identity",fill=alpha("darkblue", 0.7)) + 
  ylim(-90,120) + 
  theme_minimal() + 
  theme(
    axis.text = element_blank(),
    axis.title = element_blank(),
    panel.grid = element_blank(),
    plot.margin = unit(rep(-2,4), "cm")) + 
  coord_polar(start = 0) + 
  geom_text(data=label_data, aes(x=a, y=b+10, label=a, hjust=hjust), color="black", fontface="bold",alpha=0.6, size=2.5, angle= label_data$hjust, inherit.aes = FALSE )

I want to add a scale something like this:圆形图示例

I can't figure out how to do it without grouping everything like in this example (not mine):

empty_bar <- 10

# Add lines to the initial dataset
to_add <- matrix(NA, empty_bar, ncol(data))
colnames(to_add) <- colnames(data)
data <- rbind(data, to_add)
data$cat <- seq(1, nrow(data))

# prepare a data frame for base lines
base_data <- data %>% 
  summarize(start=min(cat), end=max() - empty_bar) %>% 
  rowwise() %>% 
  mutate(title=mean(c(start, end)))

# prepare a data frame for grid (scales)
grid_data <- base_data
grid_data$end <- grid_data$end[ c( nrow(grid_data), 1:nrow(grid_data)-1)] + 1
grid_data$start <- grid_data$start - 1
grid_data <- grid_data[-1,]

I want to be able to include a title in the figure as well. I have tried ggtitle; the labs, lab, and label functions; none will work to produce a title. Am I just crazy or is there a way to do this?

Help!!

Here the graph you are trying to reproduce is from the r-graph-gallery where they provided all the code to plot it.

Based on their code, I did the following to get a circular plot with no grouping variables for 63 values, with a scale, and a title:

1) Create a fake data:

df <- data.frame(
  individual=paste( "Cat ", 1:63, sep=""),
  count=sample( seq(10,100), 63, replace=T),
  stringsAsFactors = FALSE
)

2) Then, I wil add some empty spaces for allowing to plot the scale, calculae the angle and justificiation of each label:

library(dplyr)
df <- df %>% 
  arrange(-count) %>%
  mutate(individual = factor(individual, levels = unique(individual))) %>%
  add_row(individual = rep(NA,10),
          count = rep(NA,10)) %>%
  mutate(ID = 1:73) %>%
  mutate(angle = 90-360*(ID-0.5)/73) %>%
  mutate(hjust = ifelse(angle < -90, 1, 0)) %>%
  mutate(angle = ifelse(angle < -90, angle+180, angle)) %>%
  mutate(ID = factor(ID, unique(ID)))

3) Finally, I plot it:

library(ggplot2)
ggplot(df, aes(x = ID, y = count, fill = count))+
  geom_bar(stat = "identity")+
  geom_segment(x = 70, y = 80, xend = 1, yend = 80, colour = "grey", alpha=0.5, size=0.3 ) + # this set the scale line for y = 80
  geom_segment(x = 70, y = 60, xend = 1, yend = 60, colour = "grey", alpha=0.5, size=0.3 ) + # this set the scale line for y = 60
  geom_segment(x = 70, y = 40, xend = 1, yend = 40, colour = "grey", alpha=0.5, size=0.3 ) + # this set the scale line for y = 40
  geom_segment(x = 70, y = 20, xend = 1, yend = 20, colour = "grey", alpha=0.5, size=0.3 ) + # this set the scale line for y = 20
  annotate("text", x = rep(73,4), y = c(20, 40, 60, 80), label = c("20", "40", "60", "80") , color="grey", size=3 , angle=0, fontface="bold", hjust=1) + # Add annotation
  ylim(-100,150)+
  geom_bar(aes(x = ID, y = count, fill = count),stat = "identity")+
  geom_text(aes(label = individual, y = ifelse(count<80,90, count+ 10), hjust = hjust, angle = angle), size = 2.5)+
  coord_polar()+
  theme_minimal()+
  theme(axis.text = element_blank(),
        axis.title = element_blank(),
        panel.grid = element_blank()
        )+
  ggtitle("Title") # Add title

And I get:

在此处输入图片说明

Does it answer your question ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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