[R] R Script Modification Questions

Rasmus Liland jr@| @end|ng |rom po@teo@no
Wed Aug 19 16:42:59 CEST 2020


Dear Stephen,

I answer inline:

On 2020-08-19 12:57 +1000, Jim Lemon wrote:
| On Wed, Aug 19, 2020 at 3:09 AM Stephen P. Molnar <s.molnar using sbcglobal.net> wrote:
| |
| | What I would like to do is use 
| | linetype, rather than color, in line 
| | 27.

You need to specify linetype instead of 
color in ggplot::aes, like so

	ggplot2::aes(
	  x=date,
	  y=count,
	  linetype=cases)

and change 
scale_color_manual to 
scale_linetype_manual and its values 
like so

	  ggplot2::scale_linetype_manual(
	    name = "Test",
	    labels = levels,
	    values =
	      c("dotted",
	        "dashed",
	        "solid"))

| | The date in the title of the plot , 
| | line 33, is the max value of the 
| | date in in line 14 and I would like 
| | to use that rather than edit the 
| | Script every time the date changes.
| 
| Okay, I can't help much with the 
| ggplot stuff so forget the "lty=" 
| argument for that is base graphics. 
| However, you may get away with
| 
| ggtitle(paste0("COVID-19 Tests in Ohio \n(",date[length(date]),")"))+
| 
| I don't know whether the tidy* stuff 
| handles indexing in the same way as 
| base R.

I added a format, which converts it to 
the date format you mentioned (but with 
a zero in the month part ...):

	  ggplot2::ggtitle(
	    paste0("COVID-19 Tests in Ohio \n(",
	           format(max(dfO$date), "%m/%d/%y"), ")")) +

Here is the whole script:

	datO <- read.csv("https://api.covidtracking.com/v1/states/oh/daily.csv")
	
	cases <- c("positive", "negative", "total")
	levels <- paste0(
	  toupper(substr(cases, 1, 1)),
	  substr(cases, 2, nchar(cases)))
	
	dfO <- data.frame(
	  date=
	    rep(
	      x=lubridate::ymd(datO$date),
	      each=length(cases)),
	  cases=levels,
	  count=as.vector(t(datO[,cases])))
	dfO$cases <- factor(dfO$cases, levels=levels)
	
	file <- "/tmp/stephen.pdf"
	res <- .5
	width <- 9*res
	height <- 5*res
	pdf(file=file, width=width, height=height)
	
	mapping <- ggplot2::aes(
	  x=date,
	  y=count,
	  linetype=cases)
	
	p <- 
	  ggplot2::ggplot(
	    data=dfO,
	    mapping=mapping) +
	  ggplot2::geom_line() +
	  ggplot2::scale_linetype_manual(
	    name = "Test",
	    labels = levels,
	    values =
	      c("dotted",
	        "dashed",
	        "solid")) +
	  ggplot2::ylim(0, 2e6) +
	  ggplot2::labs(
	    x = "Date",
	    y = "Number of Tests") +
	  ggplot2::ggtitle(
	    paste0("COVID-19 Tests in Ohio \n(",
	           format(max(dfO$date), "%m/%d/%y"), ")")) +
	  ggplot2::theme_bw() +
	  ggplot2::theme(
	    axis.text.x = 
	      ggplot2::element_text(
	        angle = 30, 
	        hjust = 1),
	    plot.title = 
	      ggplot2::element_text(
	        hjust = 0.5))
	
	p
	
	dev.off()

Best,
Rasmus

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20200819/efc72779/attachment.sig>


More information about the R-help mailing list