2

I am using the windrose function posted here: Wind rose with ggplot (R)?

I need to have the percents on the figure showing on the individual lines (rather than on the left side), but so far I have not been able to figure out how. (see figure below for depiction of goal)

Here is the code that makes the figure:

  p.windrose <- ggplot(data = data,
                   aes(x = dir.binned,y = (..count..)/sum(..count..),
                       fill = spd.binned)) +
                        geom_bar()+
scale_y_continuous(breaks = ybreaks.prct,labels=percent)+
ylab("")+
scale_x_discrete(drop = FALSE,
                 labels = waiver()) +
xlab("")+
coord_polar(start = -((dirres/2)/360) * 2*pi) +
scale_fill_manual(name = "Wind Speed (m/s)", 
                  values = spd.colors,
                  drop = FALSE)+
theme_bw(base_size = 12, base_family = "Helvetica")

I marked up the figure I have so far with what I am trying to do! It'd be neat if the labels either auto-picked the location with the least wind in that direction, or if it had a tag for the placement so that it could be changed. enter image description here

I tried using geom_text, but I get an error saying that "aesthetics must be valid data columns".

Thanks for your help!

melmo
  • 757
  • 3
  • 15

2 Answers2

0

One of the things you could do is to make an extra data.frame that you use for the labels. Since the data isn't available from your question, I'll illustrate with mock data below:

library(ggplot2)

# Mock data
df <- data.frame(
  x = 1:360,
  y = runif(360, 0, 0.20)
)

labels <- data.frame(
  x = 90,
  y = scales::extended_breaks()(range(df$y))
)

ggplot(data = df,
       aes(x = as.factor(x), y = y)) +
  geom_point() +
  geom_text(data = labels,
            aes(label = scales::percent(y, 1))) +
  scale_x_discrete(breaks = seq(0, 1, length.out = 9) * 360) + 
  coord_polar() +
  theme(axis.ticks.y = element_blank(), # Disables default y-axis
        axis.text.y = element_blank())

teunbrand
  • 33,645
  • 4
  • 37
  • 63
0

@teunbrand answer got me very close! I wanted to add the code I used to get everything just right in case anyone in the future has a similar problem.

# Create the labels:
  x_location <- pi # x location of the labels

# Get the percentage
  T_data <- data %>%
    dplyr::group_by(dir.binned) %>%
    dplyr::summarise(count= n()) %>%
    dplyr::mutate(y = count/sum(count))

  labels <- data.frame(x = x_location,
                       y = scales::extended_breaks()(range(T_data$y)))
  # Create figure
  p.windrose <- ggplot() +
    geom_bar(data = data,
             aes(x = dir.binned, y = (..count..)/sum(..count..),
                 fill = spd.binned))+
    geom_text(data = labels,
              aes(x=x, y=y, label = scales::percent(y, 1))) +
    scale_y_continuous(breaks = waiver(),labels=NULL)+
    scale_x_discrete(drop = FALSE,
                     labels = waiver()) +
    ylab("")+xlab("")+
    coord_polar(start = -((dirres/2)/360) * 2*pi) +
    scale_fill_manual(name = "Wind Speed (m/s)", 
                      values = spd.colors,
                      drop = FALSE)+
    theme_bw(base_size = 12, base_family = "Helvetica") +
    theme(axis.ticks.y = element_blank(), # Disables default y-axis
          axis.text.y = element_blank())

enter image description here

melmo
  • 757
  • 3
  • 15