Last answered:

20 Jun 2024

Posted on:

10 Jun 2024


Line_chart Query


Hey there !! Thank you for teaching us data viz . Your style of teaching is very unique and keeps me motivated to learn and try new things :)

I am facing issues with the date formatting we are trying to achieve in the line chart lecture . 

PFA the screenshot of the environment .

I have tried the syntax couple of times but my Date Column seems to be formatted into NA I think that is leading to the error in the linechart generation .

Pasting the code for ease here : 

spx_ftse$Date <-as.Date(spx_ftse$Date,
                        format ="%m%d%Y")

spx_ftse_melt <- melt(spx_ftse,
                      id = "Date")

spx_ftse_melt<- rename(spx_ftse_melt,
                       c("value" = "Returns" , 
                         "variable" = "Index"))

line_chart <- ggplot(spx_ftse_melt,
                     aes(x = Date,
                         y = Returns, 
                         color = Index,
                         group = Index)) +
  geom_line(aes(color = Index), linewidth = 1) +
  scale_color_manual(values = c("navyblue", "red4")) +
  theme_minimal() +
  ggtitle("S&P vs FTSE Returns")

1 answers ( 0 marked as helpful)
Posted on:

20 Jun 2024


Hi Jeet, 

thanks for reaching out and providing your code. You can try the following steps to try and troubleshoot the problem:

 1. Confirm the Date Format in CSV
Verify the exact format of the dates in your CSV. Based on your provided screenshot, it's not possible to see the date format directly, so you would need to either:
- Open the CSV in a text editor or Excel to check the date format manually.
- Print the first few dates from R after reading the CSV but before attempting to convert them:


 2. Adjust the Date Conversion
Based on what you see, adjust the `as.Date` conversion. Common separators in date formats include slashes (`/`), hyphens (`-`), or no separator at all, and the order might vary (e.g., day before month). Here are a few examples:

- If the format is "06/20/2024" (month/day/year with slashes), you should use:

  spx_ftse$Date <- as.Date(spx_ftse$Date, format = "%m/%d/%Y")

- If the format is "20240620" (year month day with no separators), you would use:

  spx_ftse$Date <- as.Date(spx_ftse$Date, format = "%Y%m%d")

### 3. **Verify the Conversion**
After attempting the conversion, immediately check for `NA`s to confirm if the conversion was successful:


4. Test the Plot
Once you're sure the dates are correctly formatted and successfully converted (i.e., you have zero or very few `NA`s, which should be explainable), test your plot with a basic setup as previously described:

line_chart <- ggplot(spx_ftse_melt, aes(x = Date, y = Returns)) +
  geom_line() +
  ggtitle("Test Plot")

This step-by-step process should help isolate where the issue is occurring—whether during the data loading, the date conversion, or during plotting. If you still encounter issues after these steps, don't hesitate to reach out. 


365 Eli






Submit an answer