psrc / psrcplot

Package for Creating PSRC consistent data visualizations with R
https://psrc.github.io/psrcplot/
GNU General Public License v3.0
2 stars 0 forks source link

reference bar arguments not working - generating error message #15

Closed MrichardsPSRC closed 1 year ago

MrichardsPSRC commented 1 year ago

I set up the dataset to include the reference values as an additional column: image

Then using the static_bar_chart() I incorporated the reference line arguments:

own_race_chart <- static_bar_chart(t=dt_2021,
                                   x="share", y="HRACE",
                                   fill="HRACE",
                                   pos="dodge",
                                   est="percent",
                                   moe="share_moe",
                                   href="ref_val", hrefnm="Region",
                                   hrefcl='#00716c',
                                   title="Homeownership by Race and Hispanic/Latinx Origin",
                                   subtitle=NULL,
                                   source="2021 American Commmunity Survey 1-year PUMS Data",
                                   alt=NULL,
                                   xlabel=NULL, ylabel=NULL,
                                   dec = 2, color="psrc_light") + theme(legend.position = "none") #+ scale_x_discrete(limits=rev) #alphabetical order

Which produces an error message: Error in href + annot : non-numeric argument to binary operator Apparently referencing this part of the code: image

Which is referencing a part earlier in the code: image

When I use the same code but adjust it, it works:

# generate static bar chart
own_race_chart <- static_bar_chart(t=dt_2021,
                                   x="share", y="HRACE",
                                   fill="HRACE",
                                   pos="dodge",
                                   est="percent",
                                   # moe="share_moe",
                                   # href="ref_val", hrefnm="Region",
                                   # hrefcl='#00716c',
                                   title="Homeownership by Race and Hispanic/Latinx Origin",
                                   subtitle=NULL,
                                   source="2021 American Commmunity Survey 1-year PUMS Data",
                                   alt=NULL,
                                   xlabel=NULL, ylabel=NULL,
                                   dec = 2, color="psrc_light") + theme(legend.position = "none") #+ scale_x_discrete(limits=rev) #alphabetical order

# trying to figure out reference line - static
test_static <- own_race_chart + 
  ggplot2::geom_hline(yintercept= ref_val,
                      linetype='solid', linewidth=2, show.legend = FALSE, color='#00716c') +
  ggplot2::annotate("text", x = 1, y = ref_val+0.02, label = "Region", angle='270', color='#00716c')

image

MrichardsPSRC commented 1 year ago

@Ennazus @chelmann

I adjusted my code by creating a reference variable:

# setting up reference values for visualization - testing
reference_val <- dt_2021 %>% 
  filter(HRACE=="Region")

ref_val <- reference_val$share

But with the bar chart, the label looks like this: image

Based on this code:

own_race_chart <- static_bar_chart(t=dt_2021,
                                   x="share", y="HRACE",
                                   fill="HRACE",
                                   pos="dodge",
                                   est="percent",
                                   # moe="share_moe",
                                   href=ref_val, hrefnm="Region",
                                   hrefcl='#00716c',
                                   title="Homeownership by Race and Hispanic/Latinx Origin",
                                   subtitle=NULL,
                                   source="2021 American Commmunity Survey 1-year PUMS Data",
                                   alt=NULL,
                                   xlabel=NULL, ylabel=NULL,
                                   dec = 2, color="psrc_light") + theme(legend.position = "none")

Is there a way to rotate the text and adjust the color to match the line? Similar to the example I provided above when I manually added the reference line?

jensenmj commented 1 year ago

Handled/made obsolete via commit #4a562d5. Example now in the FAQ vignette.