Connecting dots on discrete data values in overlayed plot

Hi!

I'm trying to make the dots in this graph join together into a line so the graph will be easier to interperate and less messy. This is my code:
barplot(both_data_all,
col= c("#f46732", "blue3"),
xlab= "Year", ylab= "Number", main= "Number of Observers and Observations",
names.arg = group)
legend("topleft",
legend = c("Observers","Observations", "Traffic"),
fill = c("#f46732", "blue3", "black"), bty = "n")
par(mar = c(5,4,4,4)+0.3)
par(new = TRUE)
plot(vmiles, pch = 20, col= "black", cex = 1.5,
xlab= " ", ylab= " ",
lty = 1, axes = FALSE)
axis(side = 4, at = pretty(range(vmiles)))
mtext("Vehicle miles (bn)", side = 4, line = 3)

This is the graph that code makes:
example graph

I've connected the dots in their own graph by using this code:
library(tidyverse)
traffic <- tribble(
~Year, ~traffic,
2014, 322.2,
2015, 329.6,
2016, 338.3,
2017, 345.2,
2018, 349.5,
2019, 356.5,
2020, 280.5
)
traffic %>%
ggplot(aes(x = Year,
y = traffic)) +
geom_point() +
geom_line() +
theme_classic()

However I cannot successfully get this to overlay the other plot, I also can't seem to alter the axis side using this method.

Any ideas on how to solve this? Thanks!
Jem

Hi Jemma,

As you are using r base plots you can't overlay with a ggplot graphic.

I think, that maybe, this could work:

lines(traffic)

Hope it helps.

Greetings.

P.D.: I suggest you using reprex to make it easier to help.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.