尝试在 R 中复制可视化
Posted
技术标签:
【中文标题】尝试在 R 中复制可视化【英文标题】:Trying to replicate a visualisation in R 【发布时间】:2021-12-04 09:27:26 【问题描述】:相对缺乏经验的 R 用户。我正在尝试使用另一个国家/地区的数据创建类似于下面的可视化的东西。
我已经创建了基本结构,数据绘制在垂直的年度时间轴上,月份沿 x 轴运行,但我不知道如何编辑各个数据点。我将不胜感激有关如何前进甚至完全不同的方法的任何想法。
这是我使用 ggplot2 的代码:
p <- ggplot(forestfiresv, aes(y=year, x=dtstart))
p+geom_point() +
scale_x_datetime(lim=as.POSIXct(c("2021-01-01 00:01","2021-12-31 00:00", origin=lubridate::origin), "%m/%d %H:%M",tz="UTC"),expand = c(0,0), date_breaks="2 months", labels = date_format("%b"))+
theme_bw()
数据样本:
structure(list(year = c("2000", "2000", "2000", "2000", "2000",
"2000", "2000", "2000", "2000", "2000"), `Start date` = structure(c(11174, 11167, 11166, 11191,
11222, 11144, 11151, 11192, 11244, 11187), class = "Date"), `Start time` = c("02:15",
"16:05", "10:47", "15:41", "23:30", "15:29", "14:00", "13:53",
"17:39", "11:09"), `End date` = structure(c(11174,
11178, 11166, 11192, 11223, 11146, 11152, 11197, 11244, 11191
), class = "Date"), `End time` = c("14:00", "07:00", "19:00",
"22:00", "02:00", "12:00", "00:10", "13:30", "19:07", "11:30"
), Δάση = c(200, 1400, 400, 0, 0, 0, 600, 2000, 0, 260), `Forest` = c(800,
0, 0, 100, 100, 700, 0, 0, 0, 0), `Agricultural land` = c(0, 0, 0, 200, 0, 0, 200, 500, 0, 0), totalareaburnt = c(1000, 1400, 400, 500, 500, 700, 800, 2500, 350, 360), dtstart = structure(c(1628129700, 1627574700, 1627469220, 1629646860, 1632353400, 1625585340, 1626184800, 1629726780, 1634233140, 1629284940), class = c("POSIXct", "POSIXt"), tzone = "UTC"), dtend = structure(c(1628172000, 1628492400, 1627498800, 1629756000, 1632362400, 1625745600, 1626221400, 1630157400, 1634238420, 1629631800), class = c("POSIXct", "POSIXt"), tzone = "UTC")), .internal.selfref = <pointer: (nil)>, row.names = c(NA, 10L), class = c("data.table", "data.frame"))
【问题讨论】:
请您以可用的格式分享您的数据样本,例如dput(forestfiresv)
,或者如果数据太大dput(head(forestfiresv,10))
我在上面进行了编辑以包含 dput 输出。
【参考方案1】:
这是迄今为止我获得的最好成绩,但我敢打赌它可能会更好。我增加了您的示例数据框,因为只有一年的观察,并且我注入了一些随机性以使情节看起来更好。
library(ggplot2)
ddf <- rbind(df,df,df,df,df,df,df,df,df,df)
ddf$year <- rep(2000:2009,each=10)
ddf$totalareaburnt <- sample(200:2500,100,replace = T)
ddf$dtstart <- ddf$dtstart+sample(86400*1:90,100,replace = T)
#duration in days
ddf$duration <- as.numeric(df$dtend-df$dtstart)/24
ddf$year <- as.integer(ddf$year)
ggplot(ddf,
aes(y = year,
x = dtstart)) +
geom_point(aes(size = totalareaburnt,
col = duration),
shape = 17,
alpha = 0.7) +
scale_x_datetime(
lim = as.POSIXct(
c("2021-01-01 00:01", "2021-12-31 00:00", origin = lubridate::origin),
"%m/%d %H:%M",
tz = "UTC"
),
expand = c(0, 0),
date_breaks = "1 months",
labels = scales::date_format("%b")
) +
theme_minimal() +
theme(
legend.position = "top",
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
axis.line = element_line(),
axis.ticks = element_line()
) +
scale_y_continuous(trans = "reverse", breaks = unique(ddf$year))+
scale_colour_gradientn(name= "Duartion (day)",colours = c( "yellow", "orange","darkred"))+
scale_size_continuous(name="Area burned (ha)")
【讨论】:
谢谢你!你知道是否有办法只调整形状的高度以适应“totalareaburnt”而不是整个尺寸? 我已经搜索过这个但没有找到任何东西。以上是关于尝试在 R 中复制可视化的主要内容,如果未能解决你的问题,请参考以下文章