Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Having trouble making ggplot line graph

I am a beginner with R so I appreciate your patience and help in advance!

I am trying to make a line graph using ggplot to display the changes in the y variable over years. When I input my data into ggplot this is what I am getting…

The line for Y variable doesn’t reflect the changes in the data set

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is the code I used to make the graph…

ggplot(sqft2, aes(x = year, y = '100015', group = 1))+
  geom_line()

Here is the data that I am using…

   year 100015
1  1998   1504
2  1999   1504
3  2000   1504
4  2001   1504
5  2002    984
6  2003   1504
7  2004   1504
8  2005   1968
9  2006   1968
10 2007   1968
11 2008   1968
12 2009   1968
13 2010   1968
14 2011   1968
15 2012   1968
16 2013   1968
17 2014   1968
18 2015   1968
19 2016   1968
20 2017   1968
21 2018   1968
22 2019   1968
23 2020   1968
24 2021   1968


'data.frame':   24 obs. of  2 variables:
 $ year  : int  1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 ...
 $ 100015: num  1504 1504 1504 1504 984 ...

Any suggestions or help on why this is happening is greatly appreciated!

>Solution :

Try this:

A: year as numeric:

  1. Starting column names with digits is not good. Here we use rename to rename to X100015.
  2. to get year in the order we use arrange.
  3. with group=1 we get the line as desired.
library(tidyverse)
df %>% 
  rename(X100015 = `100015`) %>% 
  arrange(year) %>% 
  ggplot(aes(x=year, y=X100015, group=1)) +
  geom_line()

enter image description here

B: year as factor:

  1. Starting column names with digits is not good. Here we use rename to rename to X100015
  2. to get year in the order we use fct_inorder from forcats package (it is in tidyverse)
  3. with group=1 we get the line as desired.
library(tidyverse)

df %>% 
  rename(X100015 = `100015`) %>%
  mutate(year = factor(year)) %>%
  ggplot(aes(x=fct_inorder(year), y=X100015, group=1)) +
  geom_line()

enter image description here

data:

df <- tribble(
  ~year,  ~`100015`,
1998,   1504,
1999,   1504,
2000,   1504,
2001,   1504,
2002,    984,
2003,   1504,
2004,   1504,
2005,   1968,
2006,   1968,
2007,   1968,
2008,   1968,
2009,   1968,
2010,   1968,
2011,   1968,
2012,   1968,
2013,   1968,
2014,   1968,
2015,   1968,
2016,   1968,
2017,   1968,
2018,   1968,
2019,   1968,
2020,   1968,
2021,   1968)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading