I would like to plot the weekly values of my three variables (1) amount of tests performed (2) amount of positive tests (3) positivity rate. So far, my x-axis showed weeks. However, as this project is now running for 6 months, the x-axis is getting a bit cluttered. Note that I want to keep plotting weekly values, but I would like the xlabels to show string values "01-2021", "02-2021" etc. or "Jan-2021", "Feb-2021, (I have not made up my mind yet), so my question to you is how I can use strings in combination with xlabels.
See below some fake data that resembles my dataset. I use Stata 16.1.
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input float(total_tests total_testspos positivity_rate week_end) str7 my_date 120 30 .25 22416 "05-2021" 110 25 .22727273 22423 "05-2021" 90 13 .14444445 22430 "05-2021" 155 15 .0967742 22437 "06-2021" 124 14 .11290322 22444 "06-2021" 101 0 0 22451 "06-2021" 113 0 0 22458 "06-2021" 80 0 0 22465 "07-2021" 140 1 .007142857 22472 "07-2021" 98 5 .05102041 22479 "07-2021" 195 2 .01025641 22486 "07-2021" 109 5 .04587156 22493 "08-2021" 111 8 .072072074 22500 "08-2021" 121 11 .0909091 22507 "08-2021" 126 15 .11904762 22514 "08-2021" end format %td week_end
Code:
encode my_date, gen(my_date_new) label(my_date_new) levelsof my_date_new, local(graph_dates) clean display "`graph_dates'" twoway (scatter total_testspos week_end, c(l) m(i) ytitle("Frequency",color(ebblue) axis(1)) lcolor(ebblue) mcolor(ebblue) msize(1.5) lwidth(thick) lpattern(longdash)) || /// (scatter total_tests week_end, c(l) m(i) ytitle("Frequency", color(ebblue) axis(1)) lcolor(ebblue) mcolor(ebblue) msize(1.5) lwidth(thick) lpattern(solid)) || /// (scatter positivity_rate week_end, c(l) m(i) ytitle("Positivity rate", color(orange) axis(2)) lcolor(orange) mcolor(orange) msize(1.5) lwidth(thick) lpattern(solid) yaxis(2)), /// graphregion(color(white)) /// xlabel(`graph_dates', valuelabel angle(45)) /// ttitle("TEST xx frequencies and positivity rates") /// legend(row(3) lab(1 "no. of positive tests XX") lab(2 "no. of XX test done") lab(3 "Test XX positivity rate (%)")size(small))
Thank you and best regards,
Moniek
0 Response to Using a string variable as xlabel for plots
Post a Comment