修改element默认样式_ggplot2作图:修改主题元素的外观样式(整体修改)
一、修改基礎圖形的樣式
library(ggplot2)
library(gcookbook) # Load gcookbook for the heightweight data set
# Create the base plot
hw_plot <- ggplot(heightweight, aes(x = ageYear, y = heightIn, colour = sex)) +
geom_point()
# Options for the plotting area,修改基本圖形的外觀
hw_plot +
theme(
#設置主網格線
panel.grid.major = element_line(colour = "red"),
#設置副網格線
panel.grid.minor = element_line(colour = "red", linetype = "dashed",
size = 0.2),
#設置背景色
panel.background = element_rect(fill = "lightblue"),
#設置邊界
panel.border = element_rect(colour = "blue", fill = NA, size = 2)
)
二、設置圖例樣式
# Options for the legend,圖例
hw_plot +
theme(
#設置圖例的背景、標題、文本、小方框等
legend.background = element_rect(fill = "grey85", colour = "red", size = 1),
legend.title = element_text(colour = "blue", face = "bold", size = 14),
legend.text = element_text(colour = "red"),
legend.key = element_rect(colour = "blue", size = 0.25)
)
三、設置坐標軸和坐標軸文本樣式
# Options for text items
hw_plot +
ggtitle("Plot title here") +
theme(
axis.title.x = element_text(colour = "red", size = 14),
axis.text.x = element_text(colour = "blue"),
axis.title.y = element_text(colour = "red", size = 14, angle = 90),
axis.text.y = element_text(colour = "blue"),
plot.title = element_text(colour = "red", size = 20, face = "bold")
)
四、設置分面樣式
# Options for facets
hw_plot +
facet_grid(sex ~ .) +
theme(
strip.background = element_rect(fill = "pink"),
strip.text.y = element_text(size = 14, angle = -90, face = "bold")
)
# strip.text.x is the same, but for horizontal facets;如果是沿著x軸水平分面,那么則設置 strip.text.x
五、使用已有主題并微調細節
比如使用theme_bw(),需將其寫在前面,微調的theme()語句寫在后面,如下:
hw_plot +
theme_bw() +
theme(axis.title.x = element_text(colour = "red", size = 12))
不再貼圖。
涉及設置細節網絡查找或參考《R數據可視化手冊》9.4節。
總結
以上是生活随笔為你收集整理的修改element默认样式_ggplot2作图:修改主题元素的外观样式(整体修改)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 双层lstm每层有自己的权重参数吗_一幅
- 下一篇: 一致性hash算法虚拟节点_Hash算法