0

I'm very new to R. I want to plot graphs by months with ggplot2, but the last dates of the year variable are intertwined on the x-axis. I have attached the image below. Any ideas on how I can adjust the width on the x-axis? Can I also print each year in the date variable? My dates are between 2010-2020.

enter image description here

AndrewGB
  • 16,126
  • 5
  • 18
  • 49
Gzm_93
  • 3
  • 4
  • 1
    Welcome to SO! Please see [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Provide your data via the output of `dput(df)`, and what's your code for producing the plot in the image? – AndrewGB Dec 20 '21 at 08:01
  • I found it nice. My dataset is long 4018 rows. That's why I couldn't send it. I just added an image. This is the code I tried: `ggplot(data = uydu_ayrik) + geom_point(mapping = aes(x = Tarih, y = `00:00`)) + facet_grid(~ay)` I've been trying to write regularly but I couldn't succeed :( – Gzm_93 Dec 20 '21 at 08:41
  • With head() I can only show you the first 10 lines. Tarih 00:00 12:00 yil ay gün 1 2010-01-01 4.421524 6.198205 2010 1 1 2 2010-01-02 5.634775 6.807084 2010 1 2 3 2010-01-03 7.942957 3.676295 2010 1 3 4 2010-01-04 6.452123 5.801550 2010 1 4 5 2010-01-05 1.825242 2.573100 2010 1 5 6 2010-01-06 2.349231 4.998851 2010 1 6 7 2010-01-07 5.130694 4.827569 2010 1 7 8 2010-01-08 6.017532 5.467249 2010 1 8 9 2010-01-09 4.166854 2.848054 2010 1 9 10 2010-01-10 3.752170 4.310716 2010 1 10 – Gzm_93 Dec 20 '21 at 08:47
  • I understand you. But I can't explain myself clearly. I am attaching an image of my dataset. Because I couldn't understand how to share it on the site. [enter image description here](https://i.stack.imgur.com/ofvSp.png) – Gzm_93 Dec 20 '21 at 14:24
  • You can edit your question and then paste the results of `dput(head(df, 10))`. Adding images of the dataset is not helpful, as we can't code with it. – AndrewGB Dec 20 '21 at 17:27
  • dput(head(df,10) output max. I show the first 5 rows since the length exceeds. structure(list(Tarih = structure(c(14610, 14611, 14612, 14613, 14614), class = "Date"), `00:00` = c(4.42152416962756, 5.63477525356995, 7.94295726930555, 6.45212335222587, 1.82524197389697), `12:00` = c(6.19820454951843, 6.80708380867341, 3.67629502085304, 5.8015503251707, 2.57310045938592), yil = c(2010, 2010, 2010, 2010, 2010), ay = c(1, 1, 1, 1, 1), gün = c(1, 2, 3, 4, 5)), row.names = c(NA, 5L), class = "data.frame") – Gzm_93 Dec 20 '21 at 18:50
  • I'm trying to plot graph with the following code: 'ggplot() + geom_point(aes(x = Tarih ,y = u4$`00:00`, colour = '00:00'),data=u4,shape=19) + geom_point(aes(x = Tarih,y = u4$`12:00`, colour = '12:00'),data=u4,shape=19) + xlab(label = 'Yıl') + ylab(label = 'ERA-5_PWV') + facet_grid(.~ay)+ scale_color_manual(values = c("00:00" = "black","12:00"= "red"),name=" ")' – Gzm_93 Dec 20 '21 at 18:59
  • Sorry I was able to edit this much. When I did Ctrl+K, it didn't edit it as a code. – Gzm_93 Dec 20 '21 at 19:00

1 Answers1

0

Updated version. Op seems to be asking for this. The time variable shows the full date ("year-month-day"). Modifying the x-axis using scale_x_date for showing only calendar years:

# example dataset
dt <- data.table(date=as.Date(seq(1,1000,100),origin = "2010-01-01"),var=rnorm(10))
head(dt)

# display only the YEAR
ggplot(dt,aes(y=var,x=date))+geom_point()+
  scale_x_date(date_breaks = "1 year", date_labels =  "%Y") 

# display 6 months intervals
ggplot(dt,aes(y=var,x=date))+geom_point()+
  scale_x_date(date_breaks = "6 months", date_labels =  "%b %Y") 

Older version: the time variable shows only years.

For showing each single year of the data here are two options.

For increasing the width I guess you mean while saving the plot permanently.

Clarification: if you use R Studio you as it seems from the screenshot, you can change the temporary visualization of the plot in many ways using the GUI.

Clarification #2: check ?facet_wrap to see how you can display the facets in multiple rows and columns, that could also help the specific visualization of your plot.

library(ggplot2)
library(data.table)

# create example dataset (no values for 2015)
dt <- data.table(var=rnorm(40),year=sample(c(seq(2010,2014,1),seq(2016,2020,1)),40,replace = T))

# clearly plot each specific year by considering it as factor (2015 not shown)
ggplot(dt,aes(y=var,x=as.factor(year)))+geom_point()+
  xlab("Year") # nicer x-axis naming

# clearly plot each specific year by modifying breaks (shows also empty years if present)
ggplot(dt,aes(y=var,x=year))+geom_point()+
  scale_x_continuous(breaks = seq(min(dt[,year]),max(dt[,year]),1))

# save the file with exaggerated width (just an example)
ggsave("myfilepath/myfilename.jpg",width=20,height=4,units = "cm")
  • Thank you for the answer. I tried your suggestion. I already have the year variable in my dataset. But I don't know if the dataset is big, I didn't solve the problem. – Gzm_93 Dec 20 '21 at 10:28
  • Ok which problem specifically did not solve? – Simone Bianchi Dec 20 '21 at 12:33
  • I'm having trouble with the complex display of years I still have my own year column. That's why I didn't want to create a new column. If I do it from the date variable (in 2010-01-01 format), the years 2010, 2015 and 2020 appear on the x-axis, while the other years do not. Even if I separate it as year, month, day and only define it as Integer when I do it over the year variable, 2010.5 vs. shows values. I don't understand why it got mixed up like that. – Gzm_93 Dec 20 '21 at 13:39
  • Keep in mind I am not creating a new column! I just need an sample dataset similar to your to run a reproducible example. – Simone Bianchi Dec 20 '21 at 14:05
  • It works for times taken 3 years apart, but it doesn't work unless you reduce the size a lot when divided by 1 year periods. Thank you very much for your effort. – Gzm_93 Dec 20 '21 at 16:39