AntoineSoetewey / statsandr

A blog on statistics and R aiming at helping academics and professionals working with data to grasp important concepts in statistics and to apply them in R. See www.statsandr.com
http://statsandr.com/
35 stars 15 forks source link

blog/graphics-in-r-with-ggplot2/ #40

Closed utterances-bot closed 3 years ago

utterances-bot commented 3 years ago

Graphics in R with ggplot2 - Stats and R

Learn how to create professional graphics and plots in R (histogram, barplot, boxplot, scatter plot, line plot, density plot, etc.) with the ggplot2 package

https://statsandr.com/blog/graphics-in-r-with-ggplot2/

AntoineSoetewey commented 3 years ago

Comment written by Souradip Mookerjee on August 22, 2020 12:09:04:

Great article!

AntoineSoetewey commented 3 years ago

Comment written by Souradip Mookerjee on August 22, 2020 12:09:04:

Great article!

Comment written by Antoine Soetewey on August 22, 2020 12:11:35:

Thanks Souradip for your feedback! (and your help about commento)

AntoineSoetewey commented 3 years ago

Comment written by MiRcea Sauciuc on September 02, 2020 06:56:00:

Hi, Antoine,

Newer to your blog; love the content and your explanations. I'm curious about Axis Ticks: can that also be used for dates? I find myself in situations with continuous data (daily dates), and if I try to do any high-level EDA, the x-axis is jam-packed with (unreadable) dates. So I was looking for ways to fix the issue. Perhaps even making the data weekly/monthly.

Thank you!

AntoineSoetewey commented 3 years ago

Comment written by MiRcea Sauciuc on September 02, 2020 06:56:00:

Hi, Antoine,

Newer to your blog; love the content and your explanations. I'm curious about Axis Ticks: can that also be used for dates? I find myself in situations with continuous data (daily dates), and if I try to do any high-level EDA, the x-axis is jam-packed with (unreadable) dates. So I was looking for ways to fix the issue. Perhaps even making the data weekly/monthly.

Thank you!

Comment written by Antoine Soetewey on September 02, 2020 17:32:10:

Dear Mircea,

Good question. I'll update the blog with a section about dates, and I'll let you know when I added it.

Regards,
Antoine

AntoineSoetewey commented 3 years ago

Comment written by MiRcea Sauciuc on September 02, 2020 06:56:00: Hi, Antoine, Newer to your blog; love the content and your explanations. I'm curious about Axis Ticks: can that also be used for dates? I find myself in situations with continuous data (daily dates), and if I try to do any high-level EDA, the x-axis is jam-packed with (unreadable) dates. So I was looking for ways to fix the issue. Perhaps even making the data weekly/monthly. Thank you!

Comment written by Antoine Soetewey on September 02, 2020 17:32:10:

Dear Mircea,

Good question. I'll update the blog with a section about dates, and I'll let you know when I added it.

Regards, Antoine

Comment written by Antoine Soetewey on September 03, 2020 06:52:03:

Dear Mircea,

See this section on how to manage dates in ggplot2.

Hope this helps.

Regards,
Antoine

AntoineSoetewey commented 3 years ago

Comment written by MiRcea Sauciuc on September 02, 2020 06:56:00: Hi, Antoine, Newer to your blog; love the content and your explanations. I'm curious about Axis Ticks: can that also be used for dates? I find myself in situations with continuous data (daily dates), and if I try to do any high-level EDA, the x-axis is jam-packed with (unreadable) dates. So I was looking for ways to fix the issue. Perhaps even making the data weekly/monthly. Thank you!

Comment written by Antoine Soetewey on September 02, 2020 17:32:10: Dear Mircea, Good question. I'll update the blog with a section about dates, and I'll let you know when I added it. Regards, Antoine

Comment written by Antoine Soetewey on September 03, 2020 06:52:03:

Dear Mircea,

See this section on how to manage dates in ggplot2.

Hope this helps.

Regards, Antoine

Comment written by MiRcea Sauciuc on September 10, 2020 05:50:52:

Hello, Antoine,

Thank you very much!

Best,
Mircea

AntoineSoetewey commented 3 years ago

Comment written by Mostafa Mohammad Rezaee on November 24, 2020 04:37:27:

Great post. Thanks for sharing. I wish I had seen this post earlier.

I have "Statistical Graphics" this semester through the "Graphics for Statistics and Data Analysis with R", Second Edition, as the textbook.  Anyway, this book provides readers with all codes for its graphs through its website. A combination of this post and the book would be great.

AntoineSoetewey commented 3 years ago

Comment written by Mostafa Mohammad Rezaee on November 24, 2020 04:37:27:

Great post. Thanks for sharing. I wish I had seen this post earlier.

I have "Statistical Graphics" this semester through the "Graphics for Statistics and Data Analysis with R", Second Edition, as the textbook.  Anyway, this book provides readers with all codes for its graphs through its website. A combination of this post and the book would be great.

Comment written by Antoine Soetewey on November 24, 2020 07:17:50:

Dear Mostafa,

Glad you like it! Regarding the textbook, thanks for the suggestion too--I'll make sure to check it out.

Regards,
Antoine

Iam-MFawad commented 3 years ago

Hello Antoine Soetewey, I hope you will be fine, My name is Muhammad Fawad and I am a beginner to R programming. I have code for a graph(L-ratio diagram), and I want to convert that code to ggplot2, using the theme, colors, and other features. I mean, I want to add the feature of the ggplot2 package to my code. I will be very thankful for this kind of act.

lmomentdiagram<-function(){ tau3=seq(-1,1,.01) tau4_gev=0.10701+0.1109tau3+0.84838(tau3)^2+(-0.06669)(tau3)^3+0.00567(tau3)^4+(-0.04208)(tau3)^5+0.03673(tau3)^6+0(tau3)^7+0(tau3)^8 tau4_gpa=0+0.20196tau3+0.95924(tau3)^2+(-0.20096)(tau3)^3+0.04061(tau3)^4+0(tau3)^5+0(tau3)^6+0(tau3)^7+0(tau3)^8 tau4_glo=0.16667+0tau3+0.83333(tau3)^2 par(mar=c(5, 4, 2, 2)) plot(tau3,tau4_gev,type="l",col="royalblue",lwd=2,axes=FALSE,ann=FALSE,ylim=range(-.2,1),xlim=range(-1,1)) lines(tau3,tau4_gpa,col="tomato4",lty=1,lwd=1.5) lines(tau3,tau4_glo,col="green4",lty=1,lwd=1.5)

box(lwd=2) axis(1, las=1, at=seq(-1,1,.2),tck=-.01) axis(2, las=2, at=seq(-.2,1,.2),tck=-.01) abline(v=seq(-1,1,.2),col="black") abline(h=seq(-.2,1,.2),col="black") title(main=" Fig1 ",xlab="L-skewness",ylab="L-kurtosis") legend("bottomleft", c("Distributions","GEV","GPA", "GLO"), col = c("black","royalblue","tomato4","green4"),text.col = c("blue","green4","green4","green4"),lwd=c(-1,2,1.5,1.5), lty = c(-1,1,1,1), pch = c(-1,-1,-1, -1), merge = TRUE,cex=0.8, bg = 'gray90')

legend("bottomright", col = c("darkmagenta"),text.col = "green4",lwd=c(-1), lty = c(-1), pch = c(11),merge = TRUE,cex=0.8, bg = 'gray90')

}

lmomentdiagram() x<-c (0.11,0.154,0.144,0.676,0.333,0.664,0.789,0.234) y<-c(0.222,0.366,0.335,0.551,0.590,0.233,0.674,0.666) points(x,y,pch=9,col="blue")

AntoineSoetewey commented 3 years ago

Hello Antoine Soetewey, I hope you will be fine, My name is Muhammad Fawad and I am a beginner to R programming. I have code for a graph(L-ratio diagram), and I want to convert that code to ggplot2, using the theme, colors, and other features. I mean, I want to add the feature of the ggplot2 package to my code. I will be very thankful for this kind of act.

lmomentdiagram<-function(){ tau3=seq(-1,1,.01) tau4_gev=0.10701+0.1109tau3+0.84838(tau3)^2+(-0.06669)(tau3)^3+0.00567(tau3)^4+(-0.04208)(tau3)^5+0.03673(tau3)^6+0(tau3)^7+0(tau3)^8 tau4_gpa=0+0.20196tau3+0.95924(tau3)^2+(-0.20096)(tau3)^3+0.04061(tau3)^4+0(tau3)^5+0(tau3)^6+0(tau3)^7+0(tau3)^8 tau4_glo=0.16667+0tau3+0.83333(tau3)^2 par(mar=c(5, 4, 2, 2)) plot(tau3,tau4_gev,type="l",col="royalblue",lwd=2,axes=FALSE,ann=FALSE,ylim=range(-.2,1),xlim=range(-1,1)) lines(tau3,tau4_gpa,col="tomato4",lty=1,lwd=1.5) lines(tau3,tau4_glo,col="green4",lty=1,lwd=1.5)

box(lwd=2) axis(1, las=1, at=seq(-1,1,.2),tck=-.01) axis(2, las=2, at=seq(-.2,1,.2),tck=-.01) abline(v=seq(-1,1,.2),col="black") abline(h=seq(-.2,1,.2),col="black") title(main=" Fig1 ",xlab="L-skewness",ylab="L-kurtosis") legend("bottomleft", c("Distributions","GEV","GPA", "GLO"), col = c("black","royalblue","tomato4","green4"),text.col = c("blue","green4","green4","green4"),lwd=c(-1,2,1.5,1.5), lty = c(-1,1,1,1), pch = c(-1,-1,-1, -1), merge = TRUE,cex=0.8, bg = 'gray90')

legend("bottomright", col = c("darkmagenta"),text.col = "green4",lwd=c(-1), lty = c(-1), pch = c(11),merge = TRUE,cex=0.8, bg = 'gray90')

}

lmomentdiagram() x<-c (0.11,0.154,0.144,0.676,0.333,0.664,0.789,0.234) y<-c(0.222,0.366,0.335,0.551,0.590,0.233,0.674,0.666) points(x,y,pch=9,col="blue")

Dear Muhammad,

Thanks for your comment.

Here is your plot (with base R) versus my plot (with ggplot2):

Rplot1 Rplot2

You'll find the code here. Feel free to edit the plot according to your needs. Also, it may be a good idea to check the tau vectors in your lmomentdiagram() function as I needed to edit it slightly to make it work (there were some missing multiplication symbols *).

Hope this helps.

Regards, Antoine

AntoineSoetewey commented 3 years ago

Comment written by George Chacko on September 16, 2020 10:36:28:

Nice primer- thanks very much.

AntoineSoetewey commented 3 years ago

Comment written by George Chacko on September 16, 2020 10:36:28:

Nice primer- thanks very much.

Comment written by Antoine Soetewey on September 16, 2020 11:17:39:

Glad you like it George!

Iam-MFawad commented 3 years ago

Hello Antoine Soetewey, I hope you will be fine, My name is Muhammad Fawad and I am a beginner to R programming. I have code for a graph(L-ratio diagram), and I want to convert that code to ggplot2, using the theme, colors, and other features. I mean, I want to add the feature of the ggplot2 package to my code. I will be very thankful for this kind of act. lmomentdiagram<-function(){ tau3=seq(-1,1,.01) tau4_gev=0.10701+0.1109tau3+0.84838(tau3)^2+(-0.06669)(tau3)^3+0.00567(tau3)^4+(-0.04208)(tau3)^5+0.03673(tau3)^6+0(tau3)^7+0(tau3)^8 tau4_gpa=0+0.20196tau3+0.95924(tau3)^2+(-0.20096)(tau3)^3+0.04061(tau3)^4+0(tau3)^5+0(tau3)^6+0(tau3)^7+0(tau3)^8 tau4_glo=0.16667+0tau3+0.83333(tau3)^2 par(mar=c(5, 4, 2, 2)) plot(tau3,tau4_gev,type="l",col="royalblue",lwd=2,axes=FALSE,ann=FALSE,ylim=range(-.2,1),xlim=range(-1,1)) lines(tau3,tau4_gpa,col="tomato4",lty=1,lwd=1.5) lines(tau3,tau4_glo,col="green4",lty=1,lwd=1.5) box(lwd=2) axis(1, las=1, at=seq(-1,1,.2),tck=-.01) axis(2, las=2, at=seq(-.2,1,.2),tck=-.01) abline(v=seq(-1,1,.2),col="black") abline(h=seq(-.2,1,.2),col="black") title(main=" Fig1 ",xlab="L-skewness",ylab="L-kurtosis") legend("bottomleft", c("Distributions","GEV","GPA", "GLO"), col = c("black","royalblue","tomato4","green4"),text.col = c("blue","green4","green4","green4"),lwd=c(-1,2,1.5,1.5), lty = c(-1,1,1,1), pch = c(-1,-1,-1, -1), merge = TRUE,cex=0.8, bg = 'gray90')

legend("bottomright", col = c("darkmagenta"),text.col = "green4",lwd=c(-1), lty = c(-1), pch = c(11),merge = TRUE,cex=0.8, bg = 'gray90')

} lmomentdiagram() x<-c (0.11,0.154,0.144,0.676,0.333,0.664,0.789,0.234) y<-c(0.222,0.366,0.335,0.551,0.590,0.233,0.674,0.666) points(x,y,pch=9,col="blue")

Dear Muhammad,

Thanks for your comment.

Here is your plot (with base R) versus my plot (with ggplot2):

Rplot1 Rplot2

You'll find the code here. Feel free to edit the plot according to your needs. Also, it may be a good idea to check the tau vectors in your lmomentdiagram() function as I needed to edit it slightly to make it work (there were some missing multiplication symbols *).

Hope this helps.

Regards, Antoine

Thank you very much, now it is perfect.

CoyoteGulch commented 3 years ago

Very valuable introduction to GGPLOT2, thanks!

There are some code snippets that I do not understand in the facets section:

  1. p + facet_grid(. ~ drv)
  2. facet_wrap(~year)

I do not know what the (. ~ drv) and (~year) arguments are referencing.

John Orr

AntoineSoetewey commented 3 years ago

Very valuable introduction to GGPLOT2, thanks!

There are some code snippets that I do not understand in the facets section:

  1. p + facet_grid(. ~ drv)
  2. facet_wrap(~year)

I do not know what the (. ~ drv) and (~year) arguments are referencing.

John Orr

Dear John,

facet_grid() and facet_wrap() allow you to draw a plot for each level of the variable.

In this context, with facet_grid(. ~ drv) you draw a plot for each level of the variable drv, and with facet_wrap(~year) you draw a plot for each year.

Hope that helps.

Regards, Antoine

CoyoteGulch commented 3 years ago

Antoine, Thanks very much for the quick answer. Can you expand on what the "." and "~" are telling GGPLOT2?

AntoineSoetewey commented 3 years ago

Antoine, Thanks very much for the quick answer. Can you expand on what the "." and "~" are telling GGPLOT2?

You could read "~ year" as follows: create a multi-panel plot with one pane per year. So "~" is similar to "per" or "by".

In practice, if you want to create a multi-panel plot based on one variable you would use facet_wrap(). If you want to create a multi-panel plot based on more than one variables you would use facet_grid() because it allows for more control over the layout.

facet_grid() follows the pattern facet_grid(row_variable ~ column_variable). So with facet_grid(year ~ drv) you will create a multi-panel plot with each year as row and each level of drv as column. But with facet_grid(. ~ drv) it's like saying "I want to create a multi-panel plot with no variable as row and each level of drv as column".

Does it make more sense now?

CoyoteGulch commented 3 years ago

Antoine, Yes, it is clearer now.

Thanks.