Reputation: 183
it is a question with two missing solutions :D
Connect the points with a geom_line per variable
Change the x-axis, so that the 3 years are visible above the model
library(tidyverse)
data <- mtcars%>%
as_tibble(rownames = "model")%>%
mutate(jahr = 2019)%>%
bind_rows(mtcars%>%
as_tibble(rownames = "model")%>%
mutate_if(is.numeric, ~.*0.9)%>%
mutate(jahr = 2020))%>%
bind_rows(mtcars%>%
as_tibble(rownames = "model")%>%
mutate_if(is.numeric, ~.*0.7)%>%
mutate(jahr = 2021))
data%>%
filter(model %in% c("Datsun 710", "Honda Civic", "Valiant"))%>%
gather(variable, wert, -c(model, jahr))%>%
ggplot()+
geom_point(data = ~filter(., jahr == 2019),
aes(x = model, y = wert, col = model),
position = position_nudge(x = -.2))+
geom_point(data = ~filter(., jahr == 2020),
aes(x = model, y = wert, col = model),
position = position_nudge(x = 0))+
geom_point(data = ~filter(., jahr == 2021),
aes(x = model, y = wert, col = model),
position = position_nudge(x = .2))+
facet_wrap(~variable, scales = "free_y")
Upvotes: 0
Views: 298
Reputation: 7540
Is this what you'd like to achieve?
(Also included a possible alternative facet_grid
approach.)
library(tidyverse)
# Sample data
data <- mtcars %>%
as_tibble(rownames = "model") %>%
mutate(jahr = 2019) %>%
bind_rows(mtcars %>%
as_tibble(rownames = "model") %>%
mutate_if(is.numeric, ~ . * 0.9) %>%
mutate(jahr = 2020)) %>%
bind_rows(mtcars %>%
as_tibble(rownames = "model") %>%
mutate_if(is.numeric, ~ . * 0.7) %>%
mutate(jahr = 2021))
# Combining model & jahr
data %>%
filter(model %in% c("Datsun 710", "Honda Civic", "Valiant")) %>%
gather(variable, wert, -c(model, jahr)) %>%
mutate(yr_mod = str_c(model, " - ", jahr)) %>%
ggplot(aes(yr_mod, wert, col = model)) +
geom_point() +
geom_line(aes(group = model)) +
facet_wrap(~ variable, scales = "free_y") +
theme(axis.text.x = element_text(angle = 70, hjust = 1),
legend.position = "none") +
labs(x = NULL)
# Alternative presentation with facet_grid
data %>%
filter(model %in% c("Datsun 710", "Honda Civic", "Valiant")) %>%
gather(variable, wert, -c(model, jahr)) %>%
ggplot(aes(jahr, wert, col = model)) +
geom_point() +
geom_line(aes(group = model)) +
facet_grid(variable ~ model, scales = "free_y") +
scale_x_continuous(breaks = c(2019:2021),
expand = expansion(mult = c(0.5, 0.5))) +
theme(legend.position = "none") +
labs(x = NULL)
Created on 2022-06-12 by the reprex package (v2.0.1)
Upvotes: 1
Reputation: 79194
Something like this?
library(tidyverse)
data%>%
filter(model %in% c("Datsun 710", "Honda Civic", "Valiant"))%>%
gather(variable, wert, -c(model, jahr))%>%
ggplot(aes(x=model, y=wert, color = model, group = jahr))+
geom_point(position = position_nudge(), size=5)+
geom_line(color="grey", size=1)+
scale_color_manual(values = c("orange3", "green4", "purple4"), name = NULL) +
facet_wrap(~variable, scales = "free_y")+
theme_minimal(base_size = 20) +
theme(legend.position = "top",
axis.text.x = element_text(angle = 45, hjust=1))
Upvotes: 0