Making quarto html documents use the full window width - r

I am creating a quarto html document using R and publishing it to the web at rpubs.com. The document uses only the middle third of the window and leaves the left and right thirds blank. I am showing a table but it is not started on the far left side and as a result it is cut off part way through. How can I tell quarto to use the full window? Here is a reproducible example.
---
title: "Test2"
author: "Philip Smith"
format: html
html:
page-layout: custom
editor: visual
code-fold: true
link-external-icon: true
knitr:
opts_chunk:
collapse: true
comment: "#>"
R.options:
knitr.graphics.auto_pdf: true---
---
```{r set-options, echo=FALSE, warning=FALSE, cache=FALSE}
library(lubridate)
library(gt)
FdfT <- structure(list(Name = c("LFS participation rate", "LFS employment rate",
"LFS unemployment rate", "Job vacancy rate", "LFS payroll", "LFS payroll employment",
"LFS payroll average hours worked", "LFS payroll average hourly earnings",
"SEPH payroll", "SEPH payroll employment", "SEPH payroll average hours worked",
"SEPH payroll average hourly earnings", "LFS payroll", "LFS payroll employment",
"LFS payroll average hours worked", "LFS payroll average hourly earnings",
"SEPH payroll", "SEPH payroll employment", "SEPH payroll average hours worked",
"SEPH payroll average hourly earnings"), V1 = c(65.4, 61.5, 6,
5.6, 8.9, 5.8, 0.3, 2.7, 9.2, 6.8, -0.3, 2.3, 0.8, 0.3, 0.1,
0.4, 1.3, 0.6, 0.6, 0.1), V2 = c(65, 60.8, 6.5, 5.4, 7.7, 5.8,
-0.6, 2.3, 9.3, 7.8, -0.6, 2, -0.2, -1.5, 0.4, 0.9, 0.9, 0.1,
0, 0.9), V3 = c(65.4, 61.8, 5.5, 5.3, 9.5, 6.3, 0, 3.1, 10.1,
8, -0.6, 2.5, 1.9, 2.3, -0.3, -0.1, 0.8, 0.7, 0, 0), V4 = c(65.4,
61.9, 5.3, 5.7, 9.1, 5.1, 0.3, 3.5, 11.8, 7.2, -0.6, 4.9, 0.7,
0.3, 0.1, 0.3, 2.2, 0.8, -0.3, 1.7), V5 = c(65.3, 61.9, 5.2,
5.7, 9.3, 6.3, -0.5, 3.3, 10.2, 7, -1.2, 4.3, -0.2, 0, -0.6,
0.4, 0.2, 0.7, 0, -0.4), V6 = c(65.3, 61.9, 5.1, 5.7, 10.9, 7.2,
-0.6, 3.9, 11.5, 8.6, -0.9, 3.5, 1.2, 0.2, 0.2, 0.8, -0.4, 0,
-0.3, -0.3), V7 = c(64.9, 61.7, 4.9, 5.6, 11.3, 5.2, 0.6, 5.2,
11.2, 7.8, -0.6, 3.9, 1.3, 0.1, 0.5, 0.7, 1, 0.8, 0, 0.4), V8 = c(64.7,
61.6, 4.9, 5.5, 10.5, 4.2, 0.9, 5.2, 9, 5.9, -0.6, 3.3, 0, -0.4,
-0.1, 0.4, 0.1, 0.1, 0, -0.1), V9 = c(64.8, 61.3, 5.4, 5.3, 9.4,
3.5, 0.3, 5.4, 9.2, 5.5, 0, 3.6, 0.2, -0.1, -0.4, 0.6, 0.8, 0,
0, 0.9), V10 = c(64.7, 61.3, 5.2, 5.2, 8.5, 2.6, 0.6, 5.2, 9.1,
5.5, -0.6, 3.8, 0.5, 0.2, 0.3, 0, 0.8, 0.5, -0.3, 0.4), V11 = c(64.9,
61.6, 5.2, 4.9, 8.8, 2.8, 0.3, 5.5, 7.9, 4.7, -1.2, 4.1, 1.1,
0.5, -0.1, 0.7, -0.6, 0, -0.6, 0.1), V12 = c(64.8, 61.5, 5.1,
NA, 8.3, 2, 0.5, 5.6, NA, NA, NA, NA, 0.7, 0, 0.3, 0.5, NA, NA,
NA, NA), V13 = c(65, 61.8, 5, NA, 7.7, 2.1, 0.3, 5.2, NA, NA,
NA, NA, 0.2, 0.4, -0.1, 0, NA, NA, NA, NA)), row.names = c(NA,
-20L), class = "data.frame")
LASTdate <-"2022-12-01"
NumMths <- 12
Dates <- seq.Date(as.Date("2021-12-01"),as.Date("2022-12-01"),by="month")
colls <- c("V1","V2","V3","V4","V5","V6","V7","V8","V9","V10","V11","V12","V13")
MyTitle <- paste0("**Labour market indicators<br>",format(Dates[1],"%B %Y"),
" to ",format(Dates[13],"%B %Y"),"**")
gt_tbl1 <- gt(data=FdfT)
gt_tbl1 <- gt_tbl1 %>%
tab_options(table.font.size=12,container.width = 1450) %>%
tab_header(
title=md(html(MyTitle))
) %>%
cols_align(
align=c("left"),
columns=c(`Name`)
) %>%
fmt_number(
columns=all_of(colls),
decimals=1,
use_seps=TRUE
) %>%
cols_label(
`Name`="",
`V1`=md("**Dec<br>2021**"),
`V2`=md("**Jan<br>2022**"),
`V3`=md("**Feb<br>2022**"),
`V4`=md("**Mar<br>2022**"),
`V5`=md("**Apr<br>2022**"),
`V6`=md("**May<br>2022**"),
`V7`=md("**Jun<br>2022**"),
`V8`=md("**Jul<br>2022**"),
`V9`=md("**Aug<br>2022**"),
`V10`=md("**Sep<br>2022**"),
`V11`=md("**Oct<br>2022**"),
`V12`=md("**Nov<br>2022**"),
`V13`=md("**Dec<br>2022**")
) %>%
sub_missing(columns=everything(),rows=everything(),
missing_text="---") %>%
tab_style(
style = list(
cell_text(weight = "bold")
),
locations = cells_title()
) %>%
tab_style( # column label style
style = list(
cell_text(weight = "bold")
),
locations = cells_column_labels(
columns=c(Name,all_of(colls)))
) %>%
tab_row_group(label="Ratio, per cent",
rows=c(1:4),id="Levels") %>%
tab_row_group(label="12-month percentage change, per cent",
rows=c(5:12),id="PC12") %>%
tab_row_group(label="1-month percentage change, per cent",
rows=c(13:20),id="PC01") %>%
opt_row_striping(row_striping = TRUE) %>%
opt_vertical_padding(scale = 0.25) %>%
tab_footnote(
footnote = paste0("Dashes mean 'data not yet available'. Source for ",
"data: Statistics Canada. #PhilSmith26. Prepared: ",Sys.time()),
locations = cells_title()
)
gt_tbl1
```

I discovered that one must update to the pre-release 1.3 of quarto. Then the following YAML code does the job.
format:
html:
grid:
sidebar-width: 0px
body-width: 2000px
margin-width: 0px
gutter-width: 1.5rem

Related

is there a simple way to draw a graph in christmas tree farm in r

Is there a mathematical function or a way in which we can get a graph that will be in the form of a Christmas tree, like this?
thanks for your help
Here's one of many options:
tree <- data.frame(x = c(-5, 5, 2, 4, 1.5, 3, 0, -3, -1.5, -4, -2, -5,
-0.75, 0.75, 0.75, -0.75),
y = c(1, 1, 3, 3, 5, 5, 7, 5, 5, 3, 3, 1, 0, 0, 1, 1),
part = rep(c("branches", "trunk"), times = c(12, 4)))
baubles <- data.frame(x = c(-1.9, -2.4, 0.5, -0.3, -0.2, -1.3, 0.5,
1.2, -2.2, -1, 1.7, -1.4, -1.4, 0.4, 2.1, 0.4,
-0.8, -3.3, 0.5, -2.2, -0.1, -1.5, 2, 3.9, 1.3,
-1.7, 3.7, 2.8, 1, -0.1, 3.8, -2.9, -1.9, -1.7,
-2.6, -2.3, 0.9, 1, -0.4, 1.5, 1.8, -0.5, -1.4,
-0.4, -0.5, -0.9, -1.7, 0.7, 1.6, 1.2, -0.4, 1,
0.8, 2.3, -2.5, -2, -2.9, -1.4, -1.1, 0.2),
y = c(3, 3.3, 1.2, 4.4, 5.1, 5.2, 1.1, 6, 1.5, 2.4, 1.2,
5.4, 2.2, 3.4, 3.4, 3.8, 3.1, 1.2, 4.3,
1.6, 2.4, 5.4, 4.5, 1.1, 1.3, 5, 1.5, 1.9, 1.7,
5.4, 1.3, 1.1, 2.2, 4, 1.8, 2, 4.6, 1.1, 5.9, 4.4,
2, 1.5, 2, 1.2, 5.3, 3.6, 3.5, 4.5, 5.8, 3, 2.7,
5.3, 3.1, 1.7, 1.6, 2.8, 3.6, 2.2, 2.8, 1.7),
color = sample(c("white", "yellow", "red"), 60, TRUE))
library(ggplot2)
ggplot(tree, aes(x, y)) +
geom_polygon(aes(fill = part)) +
geom_point(data = baubles, aes(color = color), size = 4) +
scale_fill_manual(values = c("green4", "brown4"), name = "Parts of tree") +
scale_color_identity(guide = guide_legend(), labels = c("red bauble",
"white bauble", "yellow bauble"), name = "Decorations") +
theme_minimal(base_size = 20)
Created on 2022-11-20 with reprex v2.0.2

Whats the equivalent of data_color from gt package in kable?

I am trying to do this table in kable package:
To do this, I am using data_color function from gt package.
I would like to know if there is any option to do the same in kable package.
My code:
tbl_ipca %>%
gt() %>%
data_color(
columns = 1:3,
colors = scales::col_numeric(
colorspace::diverge_hcl(n = 20, palette = "Blue-Red 3"),
reverse = FALSE,
domain = c(-max(abs(tbl_ipca[1:3])), max(abs(tbl_ipca[1:3])))
)
) %>%
data_color(
columns = 4:6,
colors = scales::col_numeric(
colorspace::diverge_hcl(n = 20, palette = "Blue-Red 3"),
reverse = FALSE,
domain = c(-max(abs(tbl_ipca[4:6])), max(abs(tbl_ipca[4:6])))
)
) %>%
data_color(
columns = 7:9,
colors = scales::col_numeric(
colorspace::diverge_hcl(n = 20, palette = "Blue-Red 3"),
reverse = FALSE,
domain = c(-max(abs(tbl_ipca[7:9])), max(abs(tbl_ipca[7:9])))
)
) %>%
data_color(
columns = 10:12,
colors = scales::col_numeric(
colorspace::diverge_hcl(n = 20, palette = "Blue-Red 3"),
reverse = FALSE,
domain = c(-max(abs(tbl_ipca[10:12])), max(abs(tbl_ipca[10:12])))
)
)
My dput():
structure(list(`Set/21` = c(1.1, 1.1, 1.2, 0.9, 1.9, 0.7, 1.3,
0.3, 0.6), `Out/21` = c(1.2, 1.4, 1.2, 1.2, 1.4, 1.2, 1.3, 1.6,
1), `Nov/21` = c(0.6, 1.7, 0.9, 0.4, 2.3, 0.3, 0, 1, 0.3), `Set/21` = c(2.8,
3.5, 3, 2.4, 4.6, 2.2, 3.2, 2.1, 1.7), `Out/21` = c(3.4, 3.9,
3.3, 3, 4.3, 2.6, 3.9, 2.9, 2.1), `Nov/21` = c(2.9, 4.2, 3.4,
2.6, 5.7, 2.3, 2.6, 2.9, 2), `Set/21` = c(5.1, 5.9, 4.8, 3.6,
8.1, 2.4, 5, 4.6, 1.8), `Out/21` = c(5.9, 6.5, 5.7, 4.5, 9.2,
3.5, 6, 5.7, 2.8), `Nov/21` = c(5.5, 7.2, 5.9, 4.6, 9.4, 4, 5.2,
5.8, 3.3), `Set/21` = c(12.5, 11, 10.2, 8.4, 15.7, 5.4, 13.9,
7.8, 4.4), `Out/21` = c(12, 11.5, 10.7, 8.5, 17, 6, 13.1, 8.4,
4.9), `Nov/21` = c(10.8, 12.4, 10.7, 7.8, 19.2, 5.7, 10.4, 9.2,
4.8)), row.names = c("Comercializáveis", "Duráveis", "IPCA",
"Livres", "Monitorados", "Não Comercializáveis", "Não Duráveis",
"Semi-Duráveis", "Serviços"), class = "data.frame")

How to average 7 years of data

I'm new to R programming and this website so please bear with my incompetence. I pulled atmospheric data from the past 7 years for 7 variables; ozone, CO, NO, NO2, windspeed, PM 2.5, PM 10. What I am trying to do is graph this data to see if the government mandated stay at home orders during this covid-19 pandemic had any effect on atmospheric composition. From the graph I'm not quite sure what to do next. I believe I need an average of the past 7 years? The data is hourly data displayed in a 24 hour format.
NO.dat data frame
dput(head(NO.dat,10))
structure(list(Date = c("3/1/2014", "3/2/2014", "3/3/2014", "3/4/2014",
"3/5/2014", "3/6/2014", "3/7/2014", "3/8/2014", "3/9/2014", "3/10/2014"
), X0.00 = c(3.6, NA, 2.3, 17.1, 0.4, 0.9, 110.9, 0.1, NA, 0.4
), X1.00 = c(6.3, NA, 1.4, 18.7, 0.2, 0.2, 15.8, 0, NA, 0.6),
X2.00 = c(2.3, 0.6, 0.4, 13.9, 0.2, 0.1, 13.5, 0, 0.8, 0.3
), X3.00 = c(0.9, 0.3, 0.9, 4.2, 0.5, 0.3, 22.7, 0.2, 0.5,
0.7), X4.00 = c(0.2, 0.1, 2.8, 5.2, 0.7, 0, 40.1, 0.1, 0.8,
2.8), X5.00 = c(0.4, 0, 4.4, 11.1, 2.4, 1.8, 22.2, 0.1, 0.8,
4.1), X6.00 = c(11.8, 0.1, 17.6, 51.8, 3.6, 8.2, 2.8, 0.3,
1, 20.1), X7.00 = c(39.5, 0.6, 30.3, 118.6, 15.7, 12, 3.7,
1, 1.9, 39.1), X8.00 = c(23.9, 0.7, 25.8, 35.6, 20.6, 11.4,
6.3, 1.5, 1.2, 33.5), X9.00 = c(8.4, 1.1, 20.8, 28.7, 5.1,
9.4, 3.7, 1.3, 0.8, 9.6), X10.00 = c(4.3, 0.5, 13.3, 17.1,
1.1, 6, 1.3, 2.4, 1.4, 2.5), X11.00 = c(3.9, 0.3, 8.3, 13.9,
0.5, 5.6, 0.9, 2.3, 1.3, 1.2), X12.00 = c(4.1, 0.6, 6.3,
12.2, 0.6, 4.3, 0.8, 1.6, 1, 1.1), X13.00 = c(2.6, 0.6, 9.1,
9, 0.6, 3.6, 0.7, 2, 1.6, 1.1), X14.00 = c(3.7, 0.5, 9.3,
1.4, 0.9, 2.3, 0.9, 1.1, 1.1, 1.3), X15.00 = c(3.4, 0.5,
9.4, 0.8, 0.8, 1.8, 1.2, 1.8, 1.2, 1.1), X16.00 = c(1, 0.3,
5.7, 0.6, 2.5, 2.3, 1.1, 2.3, 1.2, 1), X17.00 = c(0.9, 0.3,
13.4, 0.5, 3.2, 1.8, 0.7, 1.4, 0.6, 0.7), X18.00 = c(0.8,
0.2, 22.1, 0.5, 3.9, 0.7, 0.7, 0.9, 0.4, 0.5), X19.00 = c(0.5,
0.2, 24.2, 1.8, 15.4, 1.1, 0.1, 0.8, 10.1, 0.6), X20.00 = c(0.5,
1, 18.4, 17.1, 5.1, 33.4, 0.3, 0.1, 45.3, 0.5), X21.00 = c(1,
0.5, 15.4, 55.7, 2, 39.5, 4.1, 0, 49.5, 0.4), X22.00 = c(0.4,
0.2, 8.1, 52.6, 2.7, 25.2, 0.9, 0.3, 27.2, 0.5), X23.00 = c(0.4,
6, 11.9, 2.2, 2.5, 62.1, 0.2, 0.1, 3.3, 0.4)), row.names = c(NA,
10L), class = "data.frame")
NO2.dat data frame
dput(head(NO2.dat,10))
structure(list(Date = c("3/1/2014", "3/2/2014", "3/3/2014", "3/4/2014",
"3/5/2014", "3/6/2014", "3/7/2014", "3/8/2014", "3/9/2014", "3/10/2014"
), X0.00 = c(5, 0.5, 3.2, 16.3, 0.4, 2, 91.2, 0.2, 0.5, 0.2),
X1.00 = c(7, 0.4, 2.4, 18.4, 0.3, 0.6, 17.7, 0.2, 0.5, 0.1
), X2.00 = c(1.7, 0.4, 0.3, 16.3, 0.1, 0.4, 10.3, 0.3, 0.6,
0.2), X3.00 = c(0.8, 0.6, 0.7, 4.4, 0.8, 0.6, 8.5, 0.4, 0.5,
0.6), X4.00 = c(0.6, 0.2, 2.6, 4.4, 1, 0.6, 43.7, 0.3, 0.7,
2.6), X5.00 = c(0.6, 0.3, 5, 12.8, 2.7, 2.8, 15.7, 0.4, 0.7,
4.3), X6.00 = c(5.8, 0.4, 18.6, 60.5, 3.8, 9.5, 3, 0.6, 0.9,
22.1), X7.00 = c(32, 0.7, 27.4, 117.5, 15.3, 12.6, 4.4, 1.7,
2.2, 36.2), X8.00 = c(21.3, 1, 22.7, 37.1, 20.3, 12.5, 7.6,
2.1, 1.4, 33.2), X9.00 = c(7.9, 1.4, 19.4, 28.7, 5, 10.5,
4.8, 2.3, 0.9, 11), X10.00 = c(4.2, 0.6, 12.4, 19, 1.6, 8.1,
1.9, 3.1, 1.8, 2.9), X11.00 = c(4.2, 0.8, 9.6, 15.7, 1.1,
7.4, 1.6, 3.4, 1.8, 1), X12.00 = c(4.2, 0.9, 6.6, 14.2, 1.2,
6.1, 1.4, 2.7, 1.3, 1.2), X13.00 = c(NA, 0.8, 9.4, NA, 1.4,
3.9, 1.2, NA, 1.9, 1.2), X14.00 = c(NA, 0.9, 9.6, NA, 1.9,
3.1, 1.3, NA, 1.3, 1.3), X15.00 = c(NA, 0.9, 9.6, NA, 1.7,
2.9, 1.9, NA, 1.6, 1), X16.00 = c(1, 0.8, 6.4, 1.2, 3.8,
3, 1.8, 3, 1.2, 1.1), X17.00 = c(1.2, 0.7, 12, 1, 4, 1.5,
1.5, 2, 0.5, 0.6), X18.00 = c(0.9, 0.5, 20.2, 0.9, 5, 1,
1.3, 1.5, 0.3, 0.3), X19.00 = c(0.5, 0.5, 19.1, 2.1, 15.8,
1.1, 0.6, 1.3, 5.1, 0.4), X20.00 = c(0.4, 1.1, 17.5, 7, 4.2,
24.9, 0.5, 0.7, 32.1, 0.4), X21.00 = c(0.7, 0.7, 13.3, 28.4,
2.4, 31.7, 3.4, 0.7, 37, 0.3), X22.00 = c(0.4, 0.4, 7.3,
21, 2.9, 18.5, 1.2, 0.6, 20, 0.3), X23.00 = c(0.4, 5.8, 11.6,
0.8, 2.9, 47.8, 0.5, 0.6, 2.1, 0.2)), row.names = c(NA, 10L
), class = "data.frame")
Any help would be much appreciated!
'''
library(reshape2)
library(dplyr)
library(lubridate)
library(ggplot2)
#remove summary stats
NO.dat <- NO.dat[,1:25]
NO2.dat <- NO2.dat[,1:25]
#reorganize data using reshape
x<-melt(NO.dat, id="Date")
colnames(x) <- c("Date","Hour","NO")
x$Hour<- as.numeric(x$Hour)-y<-melt(NO2.dat, id="Date")
y<-melt(NO2.dat, id="Date")
colnames(y) <- c("Date","Hour","NO2")
y$Hour<- as.numeric(y$Hour)-1
x <- cbind(x,y$NO2)
colnames(x)[4] <- "NO2"
x$min <- ":00"
x$time <- paste(x$Hour, x$min, sep="")
x$DT <- paste(x$Date, x$time)
x %>% select(DT, NO, NO2) %>% mutate(NOx=NO + NO2) %>% mutate(DT =
mdy_hm(DT)) %>% arrange(DT) -> x
p <- ggplot(x, aes(x=DT, y=NOx)) + geom_line() + xlab("")
x$index <- 1:nrow(x)
loessMod10 <- loess(NOx ~ index, data=x, span=0.10, na.action=)
x <- na.omit(x)
x$smoothed10 <- predict(loessMod10)
#pdf("El Paso NOx.pdf",w=6,h=3,useDingbats= FALSE)
p <- ggplot(x) + geom_line(aes(x=DT, y=NOx), linetype = "dashed",
size=0.3) + xlab("") +
geom_line(aes(x=DT, y=smoothed10), color = "red") + labs(y="NOx
(ppbv)") +
ggtitle("NOx concentrations at Chamizal TCEQ Site") +
theme(plot.title = element_text(hjust = 0.5)) +
annotate("text",x=as.POSIXct("2020-03-24 17:00:00"), y=130, +
label="Stay Home 1", angle=90, size=2.5)
annotate("text",x=as.POSIXct("2020-04-01 17:00:00"), y=130,
label="Stay Home 2", angle=90, size = 2.5) +
annotate("segment", x = as.POSIXct("2020-03-24 17:00:00"), xend=
as.POSIXct("2020-03-24 17:00:00"), y = 0, yend = 105, colour =
"blue") +
annotate("segment", x = as.POSIXct("2020-04-01 17:00:00"), xend=
as.POSIXct("2020-04-01 17:00:00"), y = 0, yend = 105, colour =
"blue")
'''
Graph that I think needs to be averaged??
Perhaps you can add a year column and use this to group your data by year. You could then overlay the data by year.
For fun, I had a go at reproducing the plots avoiding reshape.
library(dplyr)
library(tidyr)
library(stringr)
library(ggplot2)
# stack raw data for NO and NO2
NO_stacked <- NO %>%
pivot_longer(cols = starts_with("X"),
names_to = "hours",
values_to = "NO")
NO2_stacked <- NO2 %>%
pivot_longer(cols = starts_with("X"),
names_to = "hours",
values_to = "NO2")
# combine into one data frame
data <- bind_cols(NO_stacked, NO2_stacked) %>%
select(Date, hours, NO, NO2)
# coerce dates to POSIXct and add hours; remove hours; reshape to long format using pivot_longer
data <- data %>%
mutate(Date = as.POSIXct(Date, format = "%m/%d/%Y", tz = "UTC"),
hours = as.numeric(str_sub(hours, start = 2, end = -1))) %>%
mutate(Date = Date + 60*60*hours) %>%
select(-hours) %>%
pivot_longer(cols = contains("NO"),
names_to = "Contaminant",
values_to = "Concentration")
# plot
ggplot(data = data, aes(x = Date, y = Concentration)) +
geom_line() +
geom_smooth(method = "loess", formula = y ~ x) +
facet_wrap(vars(Contaminant), nrow = 2) +
ggtitle("NOx concentrations at Chamizal TCEQ Site")

How do I plot a linear regression line in a specified bin in a histogram?

So we are trying to determine speciation rate as a function of animal weight. Animal weight follows a gaussian distribution when they are plotted altogether; hence, we only want to fit the regression line in the decreasing trend of the histogram. Specifically, the line should start from x = 2.1 and y = 3.0. Fig. 1 is my current plot using the code below, while Fig. 2 is the outcome I would like to acquire (superimposed line via paint), which I don't know how to do. Any help on the matter will be greatly appreciated.
Attached is my code:
x.log = c(-2.9, -2.7, -2.5, -2.3, -2.1, -1.9, -1.7, -1.5, -1.3, -1.1,
-0.9,-0.7, -0.5, -0.3, -0.1, 0.1, 0.3, 0.5, 0.5, 0.7, 0.9, 1.1,
1.3, 1.5, 1.7, 1.9, 2.1, 2.3, 2.5, 2.7, 2.9, 3.1, 3.3, 3.5, 3.7,
3.9, 4.1, 4.3, 4.5, 4.7, 4.9, 5.1, 5.3, 5.5, 5.7, 5.9, 6.1,
6.3, 6.5,6.9, 7.1, 7.3, 7.5, 7.7, 7.9)
y.log = c(0, 0, 0, 0.47, 0.60, 0.95, 1.14, 1.38, 1.68, 1.79, 2.10, 2.26,
2.29, 2.39, 2.48, 2.52, 2.79, 2.68, 2.80, 2.84, 2.96, 2.92,
2.91, 3.01, 2.95, 3.05, 2.94, 2.96, 2.98, 2.83, 2.85, 2.83,
2.71, 2.63, 2.61, 2.57, 2.37, 2.26, 2.17, 1.99, 1.87, 1.74,
1.62, 1.36, 1.30, 1.07, 1.20, 0.90, 0.30, 0.69, 0.30, 0.47, 0
0.30, 0)
# plot the histogram
names(log.nspecies) = logbio
log.nspecies = log.nspecies[order (as.numeric(names(log.nspecies)))]
xpos = barplot(log.nspecies, las = 2, space = 0, col = 'red',
xlab = 'ln Weight', ylab = 'ln Number of species')

Unwanted lines between groups in ggplot

I have climate logger data for several years and want to plot the daily temperature cycle for each day in one month. I am using ggplot and and grouping the data by day
When I plot data from a single year, everything is fine. When I plot data from multiple years, I get lines from 23:00 back to 00:00. If I use facet_wrap, it works, but I have multiple sites and want to facet by site not year.
clim2 <- structure(list(date = structure(c(1404172980, 1404176580, 1404180180,
1404183780, 1404187380, 1404190980, 1404194580, 1404198180, 1404201780,
1404205380, 1404208980, 1404212580, 1404216180, 1404219780, 1404223380,
1404226980, 1404230580, 1404234180, 1404237780, 1404241380, 1404244980,
1404248580, 1404252180, 1404255780, 1404259380, 1404262980, 1404266580,
1404270180, 1404273780, 1404277380, 1404280980, 1404284580, 1404288180,
1404291780, 1404295380, 1404298980, 1404302580, 1404306180, 1404309780,
1404313380, 1404316980, 1404320580, 1404324180, 1404327780, 1404331380,
1404334980, 1404338580, 1404342180, 1435708980, 1435712580, 1435716180,
1435719780, 1435723380, 1435726980, 1435730580, 1435734180, 1435737780,
1435741380, 1435744980, 1435748580, 1435752180, 1435755780, 1435759380,
1435762980, 1435766580, 1435770180, 1435773780, 1435777380, 1435780980,
1435784580, 1435788180, 1435791780, 1435795380, 1435798980, 1435802580,
1435806180, 1435809780, 1435813380, 1435816980, 1435820580, 1435824180,
1435827780, 1435831380, 1435834980, 1435838580, 1435842180, 1435845780,
1435849380, 1435852980, 1435856580, 1435860180, 1435863780, 1435867380,
1435870980, 1435874580, 1435878180), class = c("POSIXct", "POSIXt"
), tzone = "NMT"), value = c(-0.1, 0, 0, 0, 0, 0, 0, 0, 0.2,
0.3, 0.7, 2.2, 2.6, 2.6, 3.3, 3, 1.9, 1.7, 1.1, 2.1, 0.7, 0.3,
-0.3, -0.4, -0.3, -1, -0.9, -1, -1, -1.1, -1.2, -0.5, -0.6, -1.2,
1.1, 3, 3.4, 4.5, 1.9, 1.9, 3.8, 3.4, 1.3, -0.1, 0.2, -0.6, -0.8,
-0.9, -0.4, -0.3, -0.3, -0.3, -0.2, -0.3, -0.6, -0.8, -0.7, -1.1,
1.2, 2.9, 1.9, 1.4, 1.7, 1.9, 1.6, 1.5, 0.9, 1.1, -0.5, -1.4,
-1.2, -1.1, -1.6, -1.3, -1.4, -1.4, -1.5, -1.3, -1.3, -1.6, -1.9,
-1.8, 0.9, 1.4, 0.9, 0.7, 0.4, -0.5, 0.1, 0.2, 0.1, -0.1, -0.6,
-0.9, -0.9, -0.7)), .Names = c("date", "value"), row.names = c(NA,
-96L), class = "data.frame")
library(ggplot2)
library(lubridate)
g <-ggplot(clim2, aes(x = hour(date) + minute(date)/60, y = value, colour = factor(year(date)), group = factor(day(date)))) +
geom_path() +
xlab("Time")
print(g)
If you want to remove these lines, you have to make sure that group contains unique value for one path (roughly speaking, some sort of non-overlapping id), e.g.
clim2$year <- year(clim2$date)
clim2$day_id <- paste0(day(clim2$date), "_", clim2$year)
ggplot(clim2, aes(x = hour(date) + minute(date)/60,
y = value, colour = factor(year), group = day_id)) +
geom_path() +
xlab("Time")

Resources