Home > Blockchain >  Setting column width in charts in R
Setting column width in charts in R

Time:12-20

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

CodePudding user response:

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")

CodePudding user response:

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

  • Related