Reputation: 23
I'd like to create two temperature curves in one plot. My Dataframe looks like this:
temp <- read.table(text = " Time Temp1 Temp2
1 00:00 18.62800 18.54458
2 00:10 18.60025 18.48283
3 00:20 18.57250 18.36767
4 00:30 18.54667 18.36950
5 00:40 18.51483 18.36550
6 00:50 18.48325 18.34783
7 01:00 18.45733 18.22625
8 01:10 18.43767 18.19067
9 01:20 18.41583 18.22042
10 01:30 18.39608 18.21225
11 01:40 18.37625 18.18658
12 01:50 18.35633 18.05942
13 02:00 18.33258 18.04142", header = T)
How can I get clean curves (no edges on the lines like a linechart) by only showing each hour (24 hours) on the x-axis? Im thinking about something with ggplot2 but I'm still learning R basics.
Upvotes: 2
Views: 795
Reputation: 1204
If you want to plot all values but only have full hours as x-axis labels you can plot the data with the ggplot
package and use the scale_x_discrete()
option to specify the breaks
.
library(ggplot2)
is_full_time <- grepl(':00', temp$Time)
ggplot(temp, aes(x = Time, y = Temp1, group = 1)) + geom_line() + scale_x_discrete(breaks = temp$Time[is_full_time])
http://ggplot2.tidyverse.org/reference/scale_discrete.html
Upvotes: 2
Reputation: 10340
We can use the tidyverse
for this. First we "clean" the data from all unnecessary rows by only taking time values with 00
minutes and then we use a ggplot
to visualize the temperatures.
library(tidyverse)
cleanTemp <- temp %>% filter(grepl(pattern = ":00", Time))
ggplot(cleanTemp, aes(x = Time, y = Temp1, group = 1)) +
geom_line()
Temp2
can be added to the plot with
geom_line(aes(y = Temp2), color = "red")
you can also use lubridate
to subset the Time column:
temp$Time <- hm(temp$Time)
cleanTemp <- temp[minute(temp$Time) == 0,]
Upvotes: 0