0

I'm trying to plot several cities' housing prices against time using ggplot (line chart). I melted the data so that it looks like:

Dates         variable       value  
2010-01-01    Shanghai       20435  
2010-02-01    Shanghai       20782  
...           Shanghai       ...  
2018-07-01    Shanghai       22491  
2010-01-01    Hangzhou       18827  
...           Hangzhou       ...  
2018-07-01    Hangzhou       29255  
...           ...            ...  

My code is as follows:

library(ggplot2)  
library(scales)  
library(reshape2)  
Housingpriceslong <- melt(Housingprices, id=c("Dates"))  
ggplot(Housingpriceslong, aes(x=Dates, y=value, colour=Housingpriceslong$variable)) + 
  geom_line() +
  scale_x_date(labels = date_format("%Y-%m"), breaks = date_breaks("6 months"))

My questions are:

  1. Something seems to be wrong and I don't get a proper graph, could you please help me correct my code?
  2. The scale_x_date function does nothing either, my x-axis scale is still shown yearly.
jay.sf
  • 60,139
  • 8
  • 53
  • 110
Vera Lee
  • 29
  • 4
  • 2
    Please read [how to make a good example](https://stackoverflow.com/q/5963269/3283824) and edit your question accordingly. – erc Aug 06 '18 at 09:59
  • Don't post screenshots of your data/code. Use `dput` to provide representative & minimal sample data. Paste your code attempt as code text (use the editing tools to properly format code). And clearly articulate what you expect as output. *"Why is the graph so messed up like that"* is not very informative. – Maurits Evers Aug 06 '18 at 10:02

1 Answers1

0

There are (potentially) two issues with your code:

  1. You need to make sure that Dates represents a Date object: convert a character vector or factor with as.Date to a Date vector.
  2. Don't use $-indexing inside aes: colour=Housingpriceslong$variable should be colour = variable).

The following works:

library(tidyverse)
df %>%
    mutate(Dates = as.Date(Dates)) %>%
    ggplot(aes(Dates, value, colour = variable)) +
    geom_line() +
    scale_x_date(
        labels = scales::date_format("%Y-%m"),
        breaks = scales::date_breaks("6 months")) +
    theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5))

enter image description here


Sample data

df <- read.table(text =
    "Dates         variable       value
2010-01-01    Shanghai       20435
2010-02-01    Shanghai       20782
2018-07-01    Shanghai       22491
2010-01-01    Hangzhou       18827
2018-07-01    Hangzhou       29255", header = T)
Maurits Evers
  • 49,617
  • 4
  • 47
  • 68
  • Thank you so much for your help!! The x-axis is working, but the graph still isn't. Housingpriceslong <- melt(Housingprices, id=c("Dates")) Housingpriceslong %>% mutate(Dates = as.Date(Dates)) %>% ggplot(aes(Dates, value, colour = variable)) + geom_line() + scale_x_date( labels = scales::date_format("%Y-%m"), breaks = scales::date_breaks("6 months")) + theme(axis.text.x = element_text(angle = 90, hjust = 1, vjust = 0.5)) – Vera Lee Aug 08 '18 at 09:59