Ggplot2: Difference between revisions
No edit summary |
|||
(4 intermediate revisions by the same user not shown) | |||
Line 4: | Line 4: | ||
==Usage== | ==Usage== | ||
;Resources | ;Resources | ||
*[http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html Top 50 ggplot2 Visualizations - The Master List] | * [http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html Top 50 ggplot2 Visualizations - The Master List] | ||
* [http://www.sthda.com/english/wiki/ggplot2-error-bars-quick-start-guide-r-software-and-data-visualization ggplot2 error bars] | |||
===Bar Chart=== | ===Bar Chart=== | ||
[[File:Ggplot2 barchart example.png|300px|thumb]] | |||
Example bar graph: | |||
<syntaxhighlight lang="R"> | |||
data(mtcars) | |||
library(ggplot2) | |||
ggplot(mtcars, aes(x=cyl, y=wt)) + | |||
geom_bar(stat="identity", fill="steelblue") + | |||
xlab("Cylinders") + | |||
ylab("Weight (1000 lbs)") + | |||
ggtitle("Car Weight vs Cylinders") + | |||
theme_gray() + | |||
theme(plot.title = element_text(hjust = 0.5)) | |||
</syntaxhighlight> | |||
===Line Graph=== | ===Line Graph=== | ||
===Pie Chart=== | ===Pie Chart=== | ||
===Saving Images=== | |||
[https://ggplot2.tidyverse.org/reference/ggsave.html ggsave documentation] | |||
<syntaxhighlight lang="R"> | |||
# Save as PDF | |||
ggsave("barchart_example.pdf", plot=plt, width=6, height=4, device="pdf") | |||
embedFonts(path.expand("barchart_example.pdf")) | |||
# Save as PNG | |||
ggsave("barchart_example.png", plot=plt, width=6, height=4, dpi=300) | |||
</syntaxhighlight> | |||
For embedding in a latex file, I recommend exporting as PDF. This allows exporting as a vector format with text. | |||
If you must export as a raster image (jpeg, png), set the dpi flag to get a high-resolution image. | |||
Note that embed fonts requires ghostscript: | |||
<pre> | |||
apt-get install ghostscript -y | |||
</pre> | |||
===Legends=== | |||
[https://ggplot2.tidyverse.org/articles/articles/faq-customising.html?q=legend#legends Customizing FAQ]<br> | |||
[https://ggplot2.tidyverse.org/reference/guide_legend.html guide_legend] | |||
<syntaxhighlight lang="R"> | |||
# Move legend to the bottom | |||
plot <- plot + theme(legend.position = "bottom") | |||
== | # Set color legend to have two rows | ||
plot <- plot + guides(color=guide_legend(nrow=2, byrow=TRUE)) | |||
</syntaxhighlight> |
Latest revision as of 20:05, 12 January 2022
ggplot2 is a plotting tool for R
Usage
- Resources
Bar Chart
Example bar graph:
data(mtcars)
library(ggplot2)
ggplot(mtcars, aes(x=cyl, y=wt)) +
geom_bar(stat="identity", fill="steelblue") +
xlab("Cylinders") +
ylab("Weight (1000 lbs)") +
ggtitle("Car Weight vs Cylinders") +
theme_gray() +
theme(plot.title = element_text(hjust = 0.5))
Line Graph
Pie Chart
Saving Images
# Save as PDF
ggsave("barchart_example.pdf", plot=plt, width=6, height=4, device="pdf")
embedFonts(path.expand("barchart_example.pdf"))
# Save as PNG
ggsave("barchart_example.png", plot=plt, width=6, height=4, dpi=300)
For embedding in a latex file, I recommend exporting as PDF. This allows exporting as a vector format with text.
If you must export as a raster image (jpeg, png), set the dpi flag to get a high-resolution image.
Note that embed fonts requires ghostscript:
apt-get install ghostscript -y
Legends
# Move legend to the bottom
plot <- plot + theme(legend.position = "bottom")
# Set color legend to have two rows
plot <- plot + guides(color=guide_legend(nrow=2, byrow=TRUE))