Benny Zuse Rousso
Benny Zuse Rousso

Reputation: 17

Geom_area not sticking with y arbitrary value

I am trying to create an area plot of twilight times in Summer/Autumn for a specific location. In my graph, I want to set the range of the times for each twilight in the x-axis, while in the y-axis I will plot other data, which is scaled from 0 to 1 and is not shown here.

The issue I am getting is that for some reason the values for two twilight categories are not remaining constant as I set below (y=1).

# Create object with times
sun.data.summer <- data.frame(h = c(0,0,
                                    0,5.23,
                                    3.75,6.05,
                                    5.05,6.78,
                                    5.87,7.38,
                                    21.07,22.57,
                                    21.67,23.38,
                                    22.40, 23.85,
                                    23.22, 24,
                                    24,24),
                              code = c("Night1","Night1",
                                       "As.dawn","As.dawn",
                                       "Nau.dawn", "Nau.dawn",
                                       "Civ.dawn","Civ.dawn",
                                       "Sunrise","Sunrise",
                                       "Sunset","Sunset",
                                       "Civ.dusk","Civ.dusk",
                                       "Nau.dusk", "Nau.dusk",
                                       "As.dusk", "As.dusk",
                                       "Night2","Night2"
                              )
)

sun.data.summer$Season_Year <- "Summer_2018"

sun.data.autumn <- data.frame(h = c(0, 5.27,
                                    5.28,6.92,
                                    6.08,7.60,
                                    6.82,8.32,
                                    7.38,8.97,
                                    17.08,21.03,
                                    17.75, 21.62,
                                    18.47, 22.35,
                                    19.15, 23.17,
                                    19.16,24),
                              code = c("Night1", "Night1",
                                       "As.dawn","As.dawn",
                                       "Nau.dawn", "Nau.dawn",
                                       "Civ.dawn","Civ.dawn",
                                       "Sunrise","Sunrise",
                                       "Sunset","Sunset",
                                       "Civ.dusk","Civ.dusk",
                                       "Nau.dusk", "Nau.dusk",
                                       "As.dusk", "As.dusk",
                                       "Night2","Night2")
)

sun.data.autumn$Season_Year <- "Autumn_2018"

sun.data <- rbind(sun.data.summer,sun.data.autumn)

g <- ggplot() + geom_area(data= sun.data, aes(y=1,x=h, fill = code, alpha=0.2)) + facet_grid(Season_Year~.) +
  scale_fill_manual(values=c("royalblue3",
                             "royalblue3",
                             "orchid3",
                             "orchid3",
                             "darkorchid3",
                             "darkorchid3",
                             "navyblue",
                             "navyblue",
                             "tan3",
                             "tan3")) +
                scale_x_continuous(breaks=c(0,2,4,6,8,10,12,14,16,18,20,22,24))

g

enter image description here

Upvotes: 0

Views: 30

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 174278

The problem is that when you have multiple times that are the same in each group, ggplot is stacking their areas. If you add position = "identity" to your geom_area call, this should resolve your problem:

ggplot() + 
  geom_area(data= sun.data[-1, ], aes(y = 1, x = h, fill = code, alpha = 0.2),
            position = "identity") + 
  facet_grid(Season_Year~.) +
  scale_fill_manual(values=c("royalblue3",
                             "royalblue3",
                             "orchid3",
                             "orchid3",
                             "darkorchid3",
                             "darkorchid3",
                             "navyblue",
                             "navyblue",
                             "tan3",
                             "tan3")) +
                scale_x_continuous(breaks=c(0,2,4,6,8,10,12,14,16,18,20,22,24))

enter image description here

Upvotes: 1

Related Questions