Dates in plot not the same as in my data sheet

Hi, i'm quite new to R and need your help.

I have an excel sheet with measurements I have taken throughout several week. I have one measurement per week and would like to plot a line graph to visualize the development. In Excel i have one column for the exact date the measurements were taken and one with the numbers. The dates are in format and it seems like R recognizes it as dates.

Now, whenever i try to plot it the dates on the x-axis are not the ones i have in my excel sheet. When I change the dates into strings i get this comment:

geom_line()`: Each group consists of only one observation.
i Do you need to adjust the group aesthetic?

The graph now has the correct dates but it doesn't have any data in it.
When I try the data_df$Date <- as.Date(data_df$Date) it doesn't change anything, it shows the wrong dates still.

This is the code I've used so far:


Read the Excel data

Algae_analysis <- read_excel("Bachelorarbeit/Datenanalyse/Algae_analysis.xlsx",
sheet = "Spots", range = "A19:B27")

Convert Week column to string format

Algae_analysis$Week <- as.character(Algae_analysis$Week)

Create a line plot

line_plot <- ggplot(Algae_analysis, aes(x = Week, y = total conc.)) +
geom_line() +
labs(x = "Week", y = "Total Concentration") +

Display the line plot


I would appreciate if anyone can help me out here as I need this for my thesis. Feel free to ask if anything is unclear.

Try not converting Week to character. Dates have an implicit order but character strings don't.

Thats what I tried first but then i get those random dates on my x-axis and not the ones i have in my table....

Can you copy and paste your data using dput()?

structure(list(Week = structure(c(19467, 19480, 19486, 19493,
19501, 19507, 19514, 19521), class = "Date"), total conc. = c(2.17,
2.315, 7.58, 5.525, 7.655, 7.145, 2.75, 4.47)), row.names = c(NA,
-8L), class = c("tbl_df", "tbl", "data.frame"))

Week total conc.

1 2023-04-20 2.170
2 2023-05-03 2.315
3 2023-05-09 7.580
4 2023-05-16 5.525
5 2023-05-24 7.655
6 2023-05-30 7.145
7 2023-06-06 2.750
8 2023-06-13 4.470

Looks the same to me. What am I missing?

You see the dates in the plot are not the ones i have in my table. I'd like those on the x-axis and not some random ones in order to make the plot clearer. I think the reason why it changes the dates is that the time spans between my sampling dates are not the same but there has to be a solution right?

Thank your for your help in advance :slight_smile:

I'm confused. As an example, the first date in the table is 2023-05-03 and the second point at about May 3. I must be missing something.

Yes, the data points in the line graph are correct, but when you look at the x-axis you see May 01, May 15 and so on and I want the exact dates from my table on the axis. The dates you see on the axis now are not the ones I have in my table.

Hope i explained my problem better now.

That's clear now. ggplot chooses "nice dates," which may not be what you want.

Use scale_x_continuous and the arguments breaks and labels to get whatever labels you want.

Okay that seems like a good solution. I couldn't make it work myself maybe you can show me?

Add in

That didn't work unfortunately. The graph still looks the same as before...

line_plot <- ggplot(Algae_analysis, aes(x = Week, y = `total conc.`)) +
  geom_line() +
  labs(x = "Week", y = "Total Concentration") +
  theme_minimal() +

Thank you so much kind stranger! You just saved me from a mental breakdown :wink:

1 Like

This topic was automatically closed 7 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.