Reputation: 1083
Given the following data frame:
structure(list(Event = c(NA, "Safari Zone Europe", "Community Day",
"Shiny Lugia Raid", "Community Day", NA, "Community Day", NA,
"Community Day", NA, NA, "Dortmund Safari Zone", "Dortmund Safari Zone",
"Community Day", "Chicago Go Fest", "Chicago Go Fest", "Chicago Go Fest",
"Zapdos Raid Day", NA, NA), Pokémon = c("Magikarp", "Magikarp, Pikachu",
"Dratini, Swablu", "Lugia", "Mareep", "Magikarp", "Charmander",
"Pichu", "Larvitar", "Wailmer", "Mawile", "Roselia", "Roselia",
"Squirtle, Squirtle (sunglasses)", "Minun, Shuppet", "Plusle",
"Minun", "Zapdos", "Mawile", "Swablu"), Date = structure(c(1502323200,
1507334400, 1519430400, 1522368000, 1523750400, 1526515200, 1526688000,
1527638400, 1529107200, 1529452800, 1529971200, 1530403200, 1530662400,
1531008000, 1531526400, 1531612800, 1531699200, 1532131200, 1532649600,
1533513600), class = c("POSIXct", "POSIXt"), tzone = "UTC"),
Catches = c(1, 7, 9, 1, 15, 1, 4, 1, 8, 1, 1, 2, 1, 4, 3,
2, 1, 1, 1, 1), `Accumulated catches` = c(1, 8, 17, 18, 33,
34, 38, 39, 47, 48, 49, 51, 52, 56, 59, 61, 62, 63, 64, 65
)), .Names = c("Event", "Pokémon", "Date", "Catches", "Accumulated catches"
), row.names = c(NA, -20L), class = c("tbl_df", "tbl", "data.frame"
))
When I try to plot my data with the "Date" on the x-axis and "Accumulated catches" on the y-axis, I use the following ggplot code:
ggplot(Shiny_List, aes(x=`Date`, y=`Accumulated catches`)) +
ggtitle("Shiny catches (dates based on GMT+1)") +
theme(plot.title = element_text(hjust = 0.5)) +
geom_point() +
geom_line() +
geom_text(aes(label=Pokémon),hjust=0, vjust=1.0)
This gives me a line between every single data point.
However, next, I'd like to differentiate between some of the data points, based on the column "Event" in the data frame, therefore I add the color=Event
to the aes()
part of my ggplot as such:
ggplot(Shiny_List, aes(x=`Date`, y=`Accumulated catches`, color=Event)) +
ggtitle("Shiny catches (dates based on GMT+1)") +
theme(plot.title = element_text(hjust = 0.5)) +
geom_point() +
geom_line() +
geom_text(aes(label=Pokémon),hjust=0, vjust=1.0)
The result from this ruins the lines between the data points. It now creates separate lines for each unique value in the "Event" column, rather than just simply one line between all the data points as in the previous graph.
Is there any way that I can keep the colors for my different data points, and also make R ignore the colors for the geom_line()
part so it creates one line for all the data points, like it did in the beginning?
For the record, I tried geom_path()
as well, but it changed nothing.
Upvotes: 0
Views: 43
Reputation: 33782
In general it's better to map properties to geoms, rather than the top level ggplot
object. This allows for finer control.
In this case, map color to the points.
ggplot(Shiny_List, aes(x = `Date`,
y = `Accumulated catches`)) +
ggtitle("Shiny catches (dates based on GMT+1)") +
theme(plot.title = element_text(hjust = 0.5)) +
geom_point(aes(color=Event)) +
geom_line() +
geom_text(aes(label = Pokémon),
hjust = 0,
vjust = 1.0)
Upvotes: 2