dumbbell graph ggplot2 adjustments

I am looking to generate a dumbbell graph showing the percent increase between current solar energy and theoretical solar energy for states in the Northeast. I have what I am looking for, there are just some minor adjustments that need to be made. For example, converting the decimals to percent values. Any suggestions are greatly appreciated.

Northeast_df <- read.csv("C:/Users/ag2546/Documents/Postdoc/Projects/Floating_Solar/Materials/Tables/FPV_Northeast_Dumbbell.csv")

Northeast_df$diff_cc <- sprintf("+%d", as.integer((Northeast_df$Climate_Crisis_pct-Northeast_df$Solar_pct_2022)*100))

Northeast_df <- arrange(Northeast_df, desc(diff_cc))
Northeast_df %>% mutate(STATE_ABBR = factor(STATE_ABBR, levels = STATE_ABBR))
Northeast_df %>% 

Northeast_df %>% filter(STATE_ABBR == "RI") -> RI_df

h <- 0.8 
h1 <- 1.55
h2 <- h1 + 1 
h3 <- 1

xColor <- "#3F5661" 
xendColor <- "#00588D"

Northeast_df %>%
  ggplot(aes(y = STATE_ABBR))+
  geom_dumbbell(aes(x = Solar_pct_2022, xend = Climate_Crisis_pct), size = 1.5, color = "#b2b2b2", 
                colour_x = xColor, colour_xend = xendColor, size_x = 2, size_xend = 2)+
  geom_text(aes(x = Solar_pct_2022 - h, label = Solar_pct_2022), color = xColor, family="sans", fontface ="bold")+
  geom_text(aes(x = Climate_Crisis_pct + h, label = Climate_Crisis_pct), color = xendColor, family = "sans", fontface = "bold")+
  geom_rect(aes(xmin = h1, xmax = h2, ymin = -Inf, ymax = Inf), fill = "#E9EDF0")+ 
  scale_y_discrete(expand = c(0.075, 0))+
  geom_text(data = RI_df, aes(label = "DIFF", x = (h1 + h2) / 2, y = "STATE_ABBR"), 
            fontface = "bold", family = "sans", vjust = h3)+
  geom_text(data = RI_df, aes(label = "Current", x = Solar_pct_2022, y = STATE_ABBR), size = 4, 
            fontface = "bold", family = "sans", vjust = h3, color = xColor)+ 
  geom_text(data = RI_df, aes(label = "Potential", x = Climate_Crisis_pct, y = STATE_ABBR), size = 4, 
            fontface = "bold", family = "sans", vjust = h3, color = xendColor)+ 
  geom_text(aes(label = diff_cc, x = (h1 + h2) / 2, y = STATE_ABBR), 
            fontface = "bold", family = "sans", color = "#758D99")+ 
  theme_minimal(base_family = "sans")+
  theme(panel.grid.minor = element_blank())+
  theme(axis.title = element_blank()) +
  theme(axis.text.x = element_blank()) + 
  theme(panel.grid.major.x = element_blank())+
  theme(plot.margin = unit(c(1.3, 1.3, 1.3, 1.3), "cm"))+
  theme(axis.text.y = element_text(size = 13, family = "sans"))+
  theme(plot.title = element_text(size = 22, family = "sans", color = "black")) +
  theme(plot.subtitle = element_text(size = 14, color = "black")) +
  labs(x = NULL, y = NULL, 
       title = "Solar energy gap",
       subtitle = "Share of utility scale solar generation\nshare if FPV is developled")

Hi @Galla1ab. One way to display decimals as percent values is to use the scales package. I don't have your data, but I believe the following updates within your two geom_text() calls would work.

label = scales::percent(Solar_pct_2022)
and 
label = scales::percent(Climate_Crisis_pct)

Hello @scottyd22, thank for the suggestion of using the scales package. That worked perfectly. As for the remaining errors, is there a efficient method for sharing data? I am new to the community.

Glad it worked! Yes, if your data frame is my_df, execute the code below and then copy and paste in the result. If your data is large, a subset is usually sufficient.

dput(my_df)
structure(list(STATE_ABBR = structure(14:1, levels = c("VA", 
"DE", "NY", "VT", "NH", "WV", "PA", "MA", "MD", "ME", "NJ", "CT", 
"DC", "RI"), class = "factor"), STATE_FIPS = c(44L, 11L, 9L, 
34L, 23L, 24L, 25L, 42L, 54L, 33L, 50L, 36L, 10L, 51L), Area_Acres = c(687251.3655, 
39472.62767, 3170829.164, 4807380.67, 20777891.52, 6310353.034, 
5173098.918, 28982915.19, 15507167.72, 5928736.826, 6153721.614, 
31051939.62, 1259538.146, 25554423.91), ACS_2021_Pop = c(1091949L, 
683154L, 3605330L, 9234024L, 1357046L, 6148545L, 6991852L, 12970650L, 
1801049L, 1372175L, 641637L, 20114745L, 981892L, 8582479L), Energy_Demand_2022 = c(7576L, 
10242L, 27767L, 74443L, 11876L, 59683L, 50983L, 145045L, 32986L, 
10818L, 5470L, 143211L, 11539L, 132265L), Energy_Supply_2022 = c(7819450.65, 
160236.94, 43054099.24, 65060636.21, 12763919.83, 37139365.37, 
21026161.33, 239261130.5, 56665360.44, 18764393.27, 2183828.65, 
125185363.2, 5308370, 89477324.62), Climate_Crisis = c(621959.43, 
10392.96, 2333001.7, 3804347.6, 5598664.1, 1555012.4, 7547010.2, 
5809901.5, 1317732.4, 3322528.8, 3074228, 18415775, 654266.4, 
9641691.9), Conservation = c(459511, 0, 1217488.9, 1718887.7, 
1034146.5, 592064.9, 2483960.8, 3396463.1, 623090.07, 941665.16, 
698719.88, 9778469.8, 191043.91, 4702845.5), Social = c(473191.01, 
10392.96, 1440634.3, 3004589, 3485525.4, 1109118.8, 5501025.4, 
3920309.5, 999775.81, 2244396.1, 2522866.7, 14799516, 455162.61, 
7436198.3), Social_Conservation = c(332550.29, 0, 705483.1, 1304227.5, 
757110.26, 290665.33, 1701282.3, 2311008.8, 508235.54, 725836.7, 
476454.02, 7654454.7, 122327.08, 3826769.4), Solar_Energy_2022 = c(361613, 
22248, 412819.89, 1518494.77, 431686.83, 713531.53, 1934183.94, 
237442.08, 0, 3908, 202124.55, 1785242.5, 63878, 4625769.16), 
    Solar_pct_2022 = c(0.04624532, 0.138844389, 0.009588399, 
    0.023339685, 0.033820867, 0.01921227, 0.091989399, 0.000992397, 
    0, 0.000208267, 0.092555132, 0.014260793, 0.012033449, 0.05169767
    ), Climate_Crisis_pct = c(0.125785362, 0.203704339, 0.063776078, 
    0.081813562, 0.472452899, 0.061081925, 0.450923685, 0.025275077, 
    0.023254637, 0.17727388, 1.500279131, 0.161368845, 0.135285295, 
    0.159453371), Conservation_pct = c(0.105010446, 0.138844389, 
    0.037866517, 0.049759465, 0.114841941, 0.035153978, 0.210126074, 
    0.01518803, 0.010995961, 0.050391886, 0.41250692, 0.092372718, 
    0.048022634, 0.104256745), Social_pct = c(0.106759931, 0.203704339, 
    0.043049424, 0.06952105, 0.306897276, 0.049075969, 0.353617059, 
    0.017377464, 0.017643509, 0.119817575, 1.247804515, 0.13248161, 
    0.097777775, 0.13480474), Social_Conservation_pct = c(0.08877392, 
    0.138844389, 0.025974367, 0.043386023, 0.093137305, 0.027038611, 
    0.172902043, 0.010651337, 0.008969069, 0.038889864, 0.310728852, 
    0.075405758, 0.035077638, 0.094465705), diff_cc = c("+7", 
    "+6", "+5", "+5", "+43", "+4", "+35", "+2", "+2", "+17", 
    "+140", "+14", "+12", "+10")), row.names = c(NA, -14L), class = "data.frame")

Hello @scottyd22 above is an example of the data I am using.

Thanks for sharing the data, @Galla1ab! I made some adjustments in the code below to get a final product , which include tweaks to h -> h3 values, adding scales::percent() as noted above, removing quotes around STATE_ABBR in the first geom_text(), adding hjust values for "Current" and "Potential", and finally condensing all of the individual theme() calls into one single theme() call. Be sure to adjust any/all of these parameters as desired. Really nice graphic!

h <- 0.1 
h1 <- 1.8 
h2 <- h1 + 0.2  
h3 <- -0.8 

xColor <- "#3F5661" 
xendColor <- "#00588D"

Northeast_df %>%
  ggplot(aes(y = STATE_ABBR))+
  geom_dumbbell(aes(x = Solar_pct_2022, xend = Climate_Crisis_pct), size = 1.5, color = "#b2b2b2", 
                colour_x = xColor, colour_xend = xendColor, size_x = 2, size_xend = 2)+
  geom_text(aes(x = Solar_pct_2022 - h, label = scales::percent(Solar_pct_2022)), color = xColor, family="sans", fontface ="bold")+
  geom_text(aes(x = Climate_Crisis_pct + h, label = scales::percent(Climate_Crisis_pct)), color = xendColor, family = "sans", fontface = "bold")+
  geom_rect(aes(xmin = h1, xmax = h2, ymin = -Inf, ymax = Inf), fill = "#E9EDF0")+ 
  scale_y_discrete(expand = c(0.075, 1))+
  geom_text(data = RI_df, aes(label = "DIFF", x = (h1 + h2) / 2, y = STATE_ABBR), 
            fontface = "bold", family = "sans", vjust = h3)+
  geom_text(data = RI_df, aes(label = "Current", x = Solar_pct_2022, y = STATE_ABBR), size = 4, 
            fontface = "bold", family = "sans", vjust = h3, color = xColor, hjust = 1)+ 
  geom_text(data = RI_df, aes(label = "Potential", x = Climate_Crisis_pct, y = STATE_ABBR), size = 4, 
            fontface = "bold", family = "sans", vjust = h3, color = xendColor, hjust = 0)+ 
  geom_text(aes(label = diff_cc, x = (h1 + h2) / 2, y = STATE_ABBR), 
            fontface = "bold", family = "sans", color = "#758D99")+ 
  theme_minimal(base_family = "sans")+
  theme(panel.grid.minor = element_blank(),
        axis.title = element_blank(),
        axis.text.x = element_blank(),
        panel.grid.major.x = element_blank(),
        plot.margin = unit(c(1.3, 1.3, 1.3, 1.3), "cm"),
        axis.text.y = element_text(size = 13, family = "sans"),
        plot.title = element_text(size = 22, family = "sans", color = "black"),
        plot.subtitle = element_text(size = 14, color = "black")) +
  labs(x = NULL, y = NULL, 
       title = "Solar energy gap",
       subtitle = "Share of utility scale solar generation\nshare if FPV is developled")

Hello @scottyd22, ahhh this is much better! Thank you. I am still new to R and ggplot, while I am able to navigate most problems some are outside my current knowledge base.

You're doing great so far. Keep it up!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.