如何使用BASIC plot()在对数线性BASE 2中进行绘制-没有ggplot?

问题描述 投票:0回答:2

在完整的披露中,我提出了类似但不同的对数线性绘图而不限制基本R绘图函数和使用时间序列的问题。

我已经能够获得我所追求的,但是在日志基础10中。我需要基础2。代码如下:

require(RCurl)
require(foreign)
require(tidyverse)
x = getURL("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv")
corona = read.csv(text = x, sep =",",header = T)
corona <- corona %>% 
  pivot_longer(cols = -c(`Province.State`, `Country.Region`, Lat, Long),
               names_to = "date",
               values_to = "cases")
corona <- corona[,c("Country.Region","date","cases")]
corona <- corona[!is.na(corona$cases), ]

Spain <- corona[corona$Country.Region=='Spain',]
Spain <- Spain[Spain$cases>1,]
startDate <- as.Date("2020-02-09")
xm <- seq(startDate, by="1 day", length.out=nrow(Spain))
#plot(Spain$cases~xm, type='l', ylab="No. Cov-19 cases", xlab='', lwd=3, col=2,
#     main = "No. Cov-19 in Spain")


Italy <- corona[corona$Country.Region=='Italy',]
Italy <- Italy[(nrow(Italy)-nrow(Spain)+1):nrow(Italy),]
plot(Italy$cases~xm, type='l', ylab="No. Cov-19 cases", xlab='', lwd=3,
    log="y", col=2, las=2, 
    main = "No. Cov-19 in Italy (red) v Spain (blue)")

lines(Spain$cases~xm, type='l', ylab="No. Cov-19 cases", xlab='', lwd=3, col=4)

[这是我想获取的但具有最新数据的图,它使撰写本文时的最大案例数为17,660,而x轴为日期:

enter image description here


两个答案的组合:

require(RCurl)
require(foreign)
require(tidyverse) # To tip the df from long row of dates to cols (pivot_longer())
x = getURL("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv")

corona = (read_csv(x)
          %>% pivot_longer(cols = -c(`Province/State`, `Country/Region`, Lat, Long),
                           names_to = "date",
                           values_to = "cases")
          %>% select(`Country/Region`, date, cases)
          %>% mutate(date=as.Date(date,format="%m/%d/%y"))
          %>% drop_na(cases)
          %>% rename(country="Country/Region")
)

cc <- (corona
       %>% filter(country %in% c("Italy","Spain"))
)

ccw <- (cc
        %>% pivot_wider(names_from="country",values_from="cases")
        ## select only since beginning of Italy/Spain epidemics
        %>% filter(cumsum(Italy>0 | Spain>0)>=2)
)


plot(ccw$date, ccw$Italy, type="l", lwd=3,
        ylab='', 
        xlab='',
        log='y',
        col=5,
        axes=FALSE,
        main = "Log-lin Cov-19 in Italy (cyan) v Spain (blue)",
        cex.main=0.9)

at1 <- seq(min(ccw$date), max(ccw$date)+1, by=3);
axis.Date(1, at=at1, format="%b %d", las=2, cex.axis=0.7)


at2 <- 2^seq(1,25,by=1)
axis(side=2, at2, cex.axis=0.7)

abline(h=at2, lty=2, col="grey90")  # Add faint grid lines
lines(ccw$date, ccw$Spain, lwd=3, col=4)

enter image description here

r plot
2个回答
1
投票
Italy$cases2 <- log2(Italy$cases)
Spain$cases2 <- log2(Spain$cases)

plot(Italy$cases2~xm, type='n', ylab="No. Cov-19 cases", xlab='',
     las=2, 
     xlim=c(min(xm), max(xm)+1), # Increase x-axis by one day
     ylim=c(1, log2(max(Italy$cases))), 
     xaxt="n", yaxt="n",  # Turn off x and y-axis
     main = "No. Cov-19 in Italy (red) v Spain (blue)",
     bty='L')  

at1 <- seq(min(xm), max(xm)+1, by=3);
axis.Date(1, at=at1, format="%b %d", las=2)

at2 <- seq(from=1, to=15, by=1); # Modify these to suit
axis(side=2, at=at2, labels=2^at2, las=1)

abline(h=at, lty=2, col="grey90")  # Add faint grid lines

lines(Italy$cases2~xm, lwd=3, col=2)
lines(Spain$cases2~xm, lwd=3, col=4)

enter image description here


2
投票

自然对数,log-10和log-2之间的差异仅在于缩放;差异仅与标记中断的方式有关...

数据操作(略微压缩/改进?)

corona = (read_csv(x)
 %>% pivot_longer(cols = -c(`Province/State`, `Country/Region`, Lat, Long),
                  names_to = "date",
                  values_to = "cases")
    %>% select(`Country/Region`, date, cases)
    %>% mutate(date=as.Date(date,format="%m/%d/%y"))
    %>% drop_na(cases)
    %>% rename(country="Country/Region")
)
cc <- (corona
    %>% filter(country %in% c("Italy","Spain"))
)
ccw <- (cc
    %>% pivot_wider(names_from="country",values_from="cases")
  ## select only since beginning of Italy/Spain epidemics
    %>% filter(cumsum(Italy>0 | Spain>0)>=1)
)

现在绘制,使用matplot()

par(las=1)
matplot(ccw$date, ccw[,-1], type="l",ylab="No. Cov-19 cases", xlab='', lwd=3,
        log="y", las=2,
        col=c("red","blue"),
        axes=FALSE,
        main = "No. Cov-19 in Italy (red) v Spain (blue)")
## custom axes
axis.Date(side=1,x=ccw$date)
axis(side=2,at=2^seq(1,14,by=2))
box()

您可以确定是否需要间隔时间。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.