line plot by ggplot

Hello,
Can anyone help me: I want just to have line plot but when I plot it looks like so as it is indicated in the picture. I am using the following code:

# Plot the 7th graph for Google mobility. Recreation & Retail
USAMobility <- USAMobility  %>% 
  dplyr::mutate(date=dmy(j))


PLOT7 <- USAMobility  %>%
  ggplot(aes(x=date, y=retail_and_recreation_percent_change_from_baseline)) +
  geom_line(aes(x=date,y=retail_and_recreation_percent_change_from_baseline, color="Google Mobility. Retail & Recreation"), size=0.75)+ 
  labs(title="", x="", y="")+theme_classic()+theme(legend.position=c(0.79,0.89), legend.justification = c("left"),legend.key.width=unit(1.25, "lines"), legend.text=element_text(size=11),legend.title=element_blank(), legend.box = "vertical",
                                                   axis.text.x = element_text(size = 10), axis.text.y = element_text(size = 10), panel.border = element_rect(colour = "black", fill=NA, size=0.5))+
  scale_color_manual(values = c("#fd0f27"))

PLOT7

Can you post a few rows of your data? Try the output of

dput(head(USAMobility,20))

Please paste that output after a line with three back ticks, like this
```
your output here
```

I'm sure you shared this image with the best intentions, but perhaps you didnt realise what it implies.
If someone wished to use example data to test code against, they would type it out from your screenshot...

This is very unlikely to happen, and so it reduces the likelihood you will receive the help you desire.
Therefore please see this guide on how to reprex data. Key to this is use of either datapasta, or dput() to share your data as code

Well, unintentionally I made mistake :frowning:

Please dont be upset.
You're welcome to try FJCC's suggestion and that of the linked to guide, and we will attempt to help you.

Hi @Rusudan1,

I happened to bump into this data set, so I think I can help you. Often, when you see a line plot like this, it means that there are groups in the data that are not declared in the plot. What's happening here is that the Google mobility data contains both country-wide data and region-specific (here, state and county level) data. That means you have a few options, depending on what you want.

If you just want the US data, filter out the subregional data with:

USAMobility %>%
  filter(is.na(sub_region_1)) %>% 
 # ...your plot code 

Alternatively, to show state data:

USAMobility %>%
  filter(!is.na(sub_region_1), is.na(sub_region_2)) %>% 
  # ...your plot code +
  facet_wrap(vars(sub_region_1))

You could take a similar approach to the county level (sub_region_2).

making a simpler reprex

Hopefully, the above answer gets at your question, but as the previous responders suggest, your original question is a bit hard to answer because 1) we didn't know where the data came from and 2) we weren't sure what you were asking for.

The art of asking for code help is about presenting code questions simply and in a way that those who want to help you can run on their machines. Copying and pasting the code in your post won't work because we don't know where the data came from, and we don't know which packages you're using. There is also a lot of detail in your plot that is not related to your question.

It so happens that your data is open. So, one option could be to tell us where to find it (https://www.google.com/covid19/mobility/) and which data you're using (2020_US_Region_Mobility_Report.csv), then including that information in your post. Or you could use one of the suggestions above for including data right in your question--something we can copy and paste.

Trying to create an example where I don't need to download something to help you is better, but since these data are open, let's just use the data as-is. If I were asking this question, I'd present an example like this:

library(tidyverse)
USAMobility <- read_csv("path/on/my/computer/2020_US_Region_Mobility_Report.csv", col_types = cols())

USAMobility %>%
  ggplot(aes(x = date, y = retail_and_recreation_percent_change_from_baseline)) +
  geom_line()
#> Warning: Removed 2 row(s) containing missing values (geom_path).

Created on 2021-02-23 by the reprex package (v0.3.0)

What's different about this example?

  1. I explicitly call library(tidyverse) since I'm using functions from the tidyverse
  2. I explicitly read in the data to USAMobility
  3. I removed everything that wasn't necessary to show my problem. All the customization in the plot, for instance, is gone.
  4. I rendered the example with the reprex package, which makes it easier to share and make sure that my example is reproducible.
4 Likes

Thank you very much, your suggestions helped me.

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.