博客地址:https://www.jianshu.com/u/619b87e54936
在BBC数据团队开发了一个R包,以ggplot2内部风格创建可发布出版物的图形,并且使新手更容易到R创建图形。例如:
通常在R中创建图表需要安装和加载某些软件包。 为了不必一一安装和加载它们,可以使用pacman软件包中的``p_load''函数通过以下代码一次加载它们。
#This line of code installs the pacman page if you do not have it installed - if you do, it simply loads the package
if(!require(pacman))install.packages("pacman")pacman::p_load('dplyr', 'tidyr', 'gapminder','ggplot2', 'ggalt','forcats', 'R.utils', 'png', 'grid', 'ggpubr', 'scales','bbplot')
bbplot
没有在CRAN上, 所以你必须通过 devtools
来安装
# install.packages('devtools')
devtools::install_github('bbc/bbplot')
共计两个函数bbc_style()
and finalise_plot()
。bbc_style()
没有参数,并且在创建绘图后将其添加到ggplot“链”中。 它所做的通常是将文本大小,字体和颜色,轴线,轴线文本,边距和许多其他标准图表组件转换为BBC样式,这是根据设计团队的建议和反馈制定的。请注意,对于折线图而言,折线的颜色或对于条形图而言是条形的颜色,并不是从bbc_style()函数中直接获得的,而是需要在其他标准ggplot图表函数中明确设置 。以下代码显示了在标准图表制作工作流程中应如何使用bbc_style()
。 这是一个非常简单的折线图的示例,使用了来自gapminder
包的数据。
#Data for chart from gapminder package
line_df <- gapminder %>%filter(country &#61;&#61; "Malawi") #Make plot
line <- ggplot(line_df, aes(x &#61; year, y &#61; lifeExp)) &#43;geom_line(colour &#61; "#1380A1", size &#61; 1) &#43;geom_hline(yintercept &#61; 0, size &#61; 1, colour&#61;"#333333") &#43;bbc_style() &#43;labs(title&#61;"Living longer",subtitle &#61; "Life expectancy in Malawi 1952-2007")
image.png
这就是bbc_style&#xff08;&#xff09;
函数实际上是在做什么。 它本质上修改了ggplot2的主题功能中的某些参数。例如&#xff0c;第一个参数是设置绘图标题元素的字体&#xff0c;大小&#xff0c;字体和颜色。
## function ()
## {
## font <- "Helvetica"
## ggplot2::theme(plot.title &#61; ggplot2::element_text(family &#61; font,
## size &#61; 28, face &#61; "bold", color &#61; "#222222"), plot.subtitle &#61; ggplot2::element_text(family &#61; font,
## size &#61; 22, margin &#61; ggplot2::margin(9, 0, 9, 0)), plot.caption &#61; ggplot2::element_blank(),
## legend.position &#61; "top", legend.text.align &#61; 0, legend.background &#61; ggplot2::element_blank(),
## legend.title &#61; ggplot2::element_blank(), legend.key &#61; ggplot2::element_blank(),
## legend.text &#61; ggplot2::element_text(family &#61; font, size &#61; 18,
## color &#61; "#222222"), axis.title &#61; ggplot2::element_blank(),
## axis.text &#61; ggplot2::element_text(family &#61; font, size &#61; 18,
## color &#61; "#222222"), axis.text.x &#61; ggplot2::element_text(margin &#61; ggplot2::margin(5,
## b &#61; 10)), axis.ticks &#61; ggplot2::element_blank(),
## axis.line &#61; ggplot2::element_blank(), panel.grid.minor &#61; ggplot2::element_blank(),
## panel.grid.major.y &#61; ggplot2::element_line(color &#61; "#cbcbcb"),
## panel.grid.major.x &#61; ggplot2::element_blank(), panel.background &#61; ggplot2::element_blank(),
## strip.background &#61; ggplot2::element_rect(fill &#61; "white"),
## strip.text &#61; ggplot2::element_text(size &#61; 22, hjust &#61; 0))
## }
##
可以使用所需的参数调用主题功能来修改图表的这些设置或添加其他主题参数。但请注意&#xff0c;要使其正常工作&#xff0c;必须是在bbc_style&#xff08;&#xff09;
后&#xff0c; 否则bbc_style&#xff08;&#xff09;
将覆盖它。
theme(panel.grid.major.x &#61; element_line(color&#61;"#cbcbcb"), panel.grid.major.y&#61;element_blank())
bbplot软件包的第二个功能finalise_plot&#xff08;&#xff09;
将使标题和副标题左对齐&#xff0c;并在图的右下角添加带有源和图像的页脚。它还会将其保存到指定的位置。该函数有五个参数&#xff1a;
finalise_plot(plot_name, source, save_filepath, width_pixels &#61; 640, height_pixels &#61; 450)
plot_name
&#xff1a;绘图的变量名称&#xff0c;例如上面的图表示例&#xff0c;plot_name
将为“ line”。source
&#xff1a;要显示在绘图左下角的源文本。save_filepath
&#xff1a;图形保存到的精确文件路径&#xff0c;包括末尾的.png
扩展名。这确实取决于工作目录以及您是否在特定的R项目中。示例文件路径为&#xff1a;&#96;&#96;Desktop / R_projects / charts / line_chart.png&#39;&#39;。width_pixels
&#xff1a;默认情况下设置为640px&#xff0c;因此仅当希望图表具有不同的宽度并指定其宽度时才调用此参数。height_pixels
&#xff1a;默认设置为450px&#xff0c;因此仅当希望图表具有不同的高度并指定其高度时才调用此参数。logo_image_path
&#xff1a;此参数指定图右下角的图像/徽标路径。默认值为占位符PNG文件&#xff0c;其背景与绘图的背景颜色匹配。
finalise_plot(plot_name &#61; my_line_plot,source &#61; "Source: Gapminder",save_filepath &#61; "filename_that_my_plot_should_be_saved_to.png",width_pixels &#61; 640,height_pixels &#61; 450,logo_image_path &#61; "placeholder.png")
因此&#xff0c;一旦创建了绘图并对其比较满意&#xff0c;就可以使用finalise_plot&#xff08;&#xff09;
函数进行最终调整并保存图表&#xff0c;以便可以在RStudio之外查看它。因为文本和其他元素的位置在RStudio的“plot”面板中无法准确呈现&#xff08;这取决于显示绘图的大小和纵横比&#xff09;&#xff0c; 因此将其保存并打开文件可以准确地表示图形的外观。
finalise_plot(plot_name &#61; line,source &#61; "Source: Gapminder",save_filepath &#61; "images/line_plot_finalised_test.png",width_pixels &#61; 640,height_pixels &#61; 550)
#Prepare data
line_df <- gapminder %>%filter(country &#61;&#61; "China") #Make plot
line <- ggplot(line_df, aes(x &#61; year, y &#61; lifeExp)) &#43;geom_line(colour &#61; "#1380A1", size &#61; 1) &#43;geom_hline(yintercept &#61; 0, size &#61; 1, colour&#61;"#333333") &#43;bbc_style() &#43;labs(title&#61;"Living longer",subtitle &#61; "Life expectancy in China 1952-2007")
#Prepare data
multiple_line_df <- gapminder %>%filter(country &#61;&#61; "China" | country &#61;&#61; "United States")
#Make plot
multiple_line <- ggplot(multiple_line_df, aes(x &#61; year, y &#61; lifeExp, colour &#61; country)) &#43;geom_line(size &#61; 1) &#43;geom_hline(yintercept &#61; 0, size &#61; 1, colour&#61;"#333333") &#43;scale_colour_manual(values &#61; c("#FAAB18", "#1380A1")) &#43;bbc_style() &#43;labs(title&#61;"Living longer",subtitle &#61; "Life expectancy in China and the US")
#Prepare data
bar_df <- gapminder %>%filter(year &#61;&#61; 2007 & continent &#61;&#61; "Africa") %>%arrange(desc(lifeExp)) %>%head(5)#Make plot
bars <- ggplot(bar_df, aes(x &#61; country, y &#61; lifeExp)) &#43;geom_bar(stat&#61;"identity", position&#61;"identity", fill&#61;"#1380A1") &#43;geom_hline(yintercept &#61; 0, size &#61; 1, colour&#61;"#333333") &#43;bbc_style() &#43;labs(title&#61;"Reunion is highest",subtitle &#61; "Highest African life expectancy, 2007")
#prepare data
stacked_df <- gapminder %>% filter(year &#61;&#61; 2007) %>%mutate(lifeExpGrouped &#61; cut(lifeExp, breaks &#61; c(0, 50, 65, 80, 90),labels &#61; c("Under 50", "50-65", "65-80", "80&#43;"))) %>%group_by(continent, lifeExpGrouped) %>%summarise(continentPop &#61; sum(as.numeric(pop)))#set order of stacks by changing factor levels
stacked_df$lifeExpGrouped &#61; factor(stacked_df$lifeExpGrouped, levels &#61; rev(levels(stacked_df$lifeExpGrouped)))#create plot
stacked_bars <- ggplot(data &#61; stacked_df, aes(x &#61; continent,y &#61; continentPop,fill &#61; lifeExpGrouped)) &#43;geom_bar(stat &#61; "identity", position &#61; "fill") &#43;bbc_style() &#43;scale_y_continuous(labels &#61; scales::percent) &#43;scale_fill_viridis_d(direction &#61; -1) &#43;geom_hline(yintercept &#61; 0, size &#61; 1, colour &#61; "#333333") &#43;labs(title &#61; "How life expectancy varies",subtitle &#61; "% of population by life expectancy band, 2007") &#43;theme(legend.position &#61; "top", legend.justification &#61; "left") &#43;guides(fill &#61; guide_legend(reverse &#61; TRUE))
#Prepare data
grouped_bar_df <- gapminder %>%filter(year &#61;&#61; 1967 | year &#61;&#61; 2007) %>%select(country, year, lifeExp) %>%spread(year, lifeExp) %>%mutate(gap &#61; &#96;2007&#96; - &#96;1967&#96;) %>%arrange(desc(gap)) %>%head(5) %>%gather(key &#61; year, value &#61; lifeExp,-country,-gap) #Make plot
grouped_bars <- ggplot(grouped_bar_df, aes(x &#61; country, y &#61; lifeExp, fill &#61; as.factor(year))) &#43;geom_bar(stat&#61;"identity", position&#61;"dodge") &#43;geom_hline(yintercept &#61; 0, size &#61; 1, colour&#61;"#333333") &#43;bbc_style() &#43;scale_fill_manual(values &#61; c("#1380A1", "#FAAB18")) &#43;labs(title&#61;"We&#39;re living longer",subtitle &#61; "Biggest life expectancy rise, 1967-2007")
表示差异的另一种方式是哑铃图&#xff1a;
library("ggalt")
library("tidyr")#Prepare data
dumbbell_df <- gapminder %>%filter(year &#61;&#61; 1967 | year &#61;&#61; 2007) %>%select(country, year, lifeExp) %>%spread(year, lifeExp) %>%mutate(gap &#61; &#96;2007&#96; - &#96;1967&#96;) %>%arrange(desc(gap)) %>%head(10)#Make plot
ggplot(dumbbell_df, aes(x &#61; &#96;1967&#96;, xend &#61; &#96;2007&#96;, y &#61; reorder(country, gap), group &#61; country)) &#43; geom_dumbbell(colour &#61; "#dddddd",size &#61; 3,colour_x &#61; "#FAAB18",colour_xend &#61; "#1380A1") &#43;bbc_style() &#43; labs(title&#61;"We&#39;re living longer",subtitle&#61;"Biggest life expectancy rise, 1967-2007")
hist_df <- gapminder %>%filter(year &#61;&#61; 2007)ggplot(hist_df, aes(lifeExp)) &#43;geom_histogram(binwidth &#61; 5, colour &#61; "white", fill &#61; "#1380A1") &#43;geom_hline(yintercept &#61; 0, size &#61; 1, colour&#61;"#333333") &#43;bbc_style() &#43;scale_x_continuous(limits &#61; c(35, 95),breaks &#61; seq(40, 90, by &#61; 10),labels &#61; c("40", "50", "60", "70", "80", "90 years")) &#43;labs(title &#61; "How life expectancy varies",subtitle &#61; "Distribution of life expectancy in 2007")
仅仅移除特定legend
multiple_line &#43; guides(colour&#61;FALSE)
移除所有legend
multiple_line &#43; theme(legend.position &#61; "none")
image.png
multiple_line &#43; theme(legend.position &#61; "right")
为了确切地说明我们希望图例的位置&#xff0c;我们可以给它指定特定的坐标&#xff0c;例如legend.position &#61; c&#xff08;0.98,0.1&#xff09;
将图例移到右下角。 作为参考&#xff0c;c&#xff08;0,0&#xff09;在左下方&#xff0c;c&#xff08;1,0&#xff09;在右下方&#xff0c;c&#xff08;0,1&#xff09;在左上方&#xff0c;依此类推。 找到最佳的位置可能会涉及一些反复试验。要检查图例在最终绘图中出现的确切位置&#xff0c;必须查看保存的文件。
multiple_line &#43; theme(legend.position &#61; c(0.115,1.05),legend.direction &#61; "horizontal") &#43; labs(title&#61;"Living longer",subtitle &#61; "Life expectancy in China and the USn")
为了使图例与图表的左侧齐平&#xff0c;使用legend.margin
为图例设置负的左边距会更容易。 语法是margin(top, right, bottom, left)。
&#43; theme(legend.margin &#61; margin(0, 0, 0, -200))
&#43; theme(legend.title &#61; element_blank())
&#43; guides(fill &#61; guide_legend(reverse &#61; TRUE))
可以指定您的图例作为guide的参数的行数。 例如&#xff0c;下面的代码片段将创建具有4行的图例&#xff1a;
&#43; guides(fill &#61; guide_legend(nrow &#61; 4, byrow &#61; T))
可以通过将参数override.aes添加到guide中来更改图例符号的默认外观&#xff0c;例如下面将增加图例符号的大小&#xff1a;
&#43; guides(fill &#61; guide_legend(override.aes &#61; list(size &#61; 4))))
默认的ggplot图例在各个图例项目之间几乎没有空格。 可以通过手动更改比例标签来添加空间。
&#43; scale_colour_manual(labels &#61; function(x) paste0(" ", x, " "))
如果图例显示的内容有所不同&#xff0c;则需要相应地更改代码。 例如scale_fill_manual&#xff08;&#xff09;
。
添加coord_flip&#xff08;&#xff09;
使垂直分布变为水平&#xff1a;
bars <- bars &#43; coord_flip()
默认主题的y轴具有网格线。 用panel.grid.major.x &#61; element_line添加x条网格线。&#xff08;类似地&#xff0c;通过panel.grid.major.y &#61; element_blank&#xff08;&#xff09;
删除y轴上的网格线&#xff09;
bars <- bars &#43; coord_flip() &#43;theme(panel.grid.major.x &#61; element_line(color&#61;"#cbcbcb"), panel.grid.major.y&#61;element_blank())## Coordinate system already present. Adding new coordinate system, which will replace the existing one.
可以使用scale_y_continuous或scale_x_continuous
自由更改轴文本标签&#xff1a;
bars <- bars &#43; scale_y_continuous(limits&#61;c(0,85),breaks &#61; seq(0, 80, by &#61; 20),labels &#61; c("0","20", "40", "60", "80 years"))bars
可以指定轴文本具有千位分隔符&#xff0c;并带有scale_y_continuous
的参数。有两种方法可以执行此操作&#xff0c;一种在基数R中有点儿麻烦&#xff1a;
&#43; scale_y_continuous(labels &#61; function(x) format(x, big.mark &#61; ",",scientific &#61; FALSE))
第二种方法依赖于scales包&#xff0c;但是更加简洁&#xff1a;
&#43; scale_y_continuous(labels &#61; scales::comma)
这也很容易通过在scale_y_continuous中添加参数来实现&#xff1a;
&#43; scale_y_continuous(labels &#61; function(x) paste0(x, "%"))
bars &#43; ylim(c(0,500))## Scale for &#39;y&#39; is already present. Adding another scale for &#39;y&#39;, which
## will replace the existing scale.
默认主题没有轴标题&#xff0c;但是可以通过修改theme&#xff08;&#xff09;
来完成。请注意&#xff0c;必须在bbc_style&#xff08;&#xff09;
后之后执行此操作&#xff0c;否则更改将被覆盖&#xff1a;
&#43; theme(axis.title &#61; element_text(size &#61; 18))
如果添加轴标题&#xff0c;则默认情况下&#xff0c;它们将是数据集中的列名。 您可以在调用labs&#xff08;&#xff09;时将其更改为所需的任何内容。例如&#xff0c;如果您希望x轴标题为“ I&#39;m a axis”&#xff0c;而y轴标签为空白&#xff0c;则格式为&#xff1a;
&#43; labs(x &#61; "I&#39;m an axis", y &#61; "")
可以通过在主题中添加axis.ticks.x或axis.ticks.y来添加轴刻度线&#xff1a;
multiple_line &#43; theme(axis.ticks.x &#61; element_line(colour &#61; "#333333"), axis.ticks.length &#61; unit(0.26, "cm"))
向绘图添加文本注释的最简单方法是使用geom_label
&#xff1a;
multiple_line &#43; geom_label(aes(x &#61; 1980, y &#61; 45, label &#61; "I&#39;m an annotation!"), hjust &#61; 0, vjust &#61; 0.5, colour &#61; "#555555", fill &#61; "white", label.size &#61; NA, family&#61;"Helvetica", size &#61; 6)
使用 n在标签中的必要位置添加换行&#xff0c;并使用lineheight
设置行高。
multiple_line <- multiple_line &#43; geom_label(aes(x &#61; 1980, y &#61; 45, label &#61; "I&#39;m quite a longnannotation overnthree rows"), hjust &#61; 0, vjust &#61; 0.5, lineheight &#61; 0.8,colour &#61; "#555555", fill &#61; "white", label.size &#61; NA, family&#61;"Helvetica", size &#61; 6) multiple_line <- multiple_line &#43; theme(legend.position &#61; "none") &#43; xlim(c(1950, 2011)) &#43;geom_label(aes(x &#61; 2007, y &#61; 79, label &#61; "US"), hjust &#61; 0, vjust &#61; 0.5, colour &#61; "#1380A1", fill &#61; "white", label.size &#61; NA, family&#61;"Helvetica", size &#61; 6) &#43;geom_label(aes(x &#61; 2007, y &#61; 72, label &#61; "China"), hjust &#61; 0, vjust &#61; 0.5, colour &#61; "#FAAB18", fill &#61; "white", label.size &#61; NA, family&#61;"Helvetica", size &#61; 6)
参数&#39;hjust&#39;和&#39;vjust&#39;指示水平和垂直文本对齐方式。 它们的值可以在0到1之间&#xff0c;其中0左对齐&#xff0c;而1右对齐&#xff08;或垂直对齐的底部和顶部对齐&#xff09;。
上面的向图表添加注释的方法使您可以精确地指定x和y坐标。 如果我们想在特定位置添加文本注释&#xff0c;这将非常有用&#xff0c;但是重复起来将非常繁琐。幸运的是&#xff0c;如果要向所有数据点添加标签&#xff0c;则可以简单地根据数据设置位置。假设我们要在条形图中添加数据标签&#xff1a;
labelled.bars <- bars &#43;geom_label(aes(x &#61; country, y &#61; lifeExp, label &#61; round(lifeExp, 0)),hjust &#61; 1, vjust &#61; 0.5, colour &#61; "white", fill &#61; NA, label.size &#61; NA, family&#61;"Helvetica", size &#61; 6)labelled.bars
上面的代码会自动为每个大洲添加一个文本标签&#xff0c;而无需我们分别添加“ geom_label”。
&#xff08;如果您对为什么将x设置为大陆&#xff0c;将y设置为预期寿命感到困惑&#xff0c;那么当图表似乎正以相反的方式绘制它们时&#xff0c;这是因为我们已经翻转了 使用coord_flip&#xff08;&#xff09;
进行绘图。
如果您想为条形图添加左对齐标签&#xff0c;只需根据数据设置x
参数&#xff0c;而是直接使用数字值指定y
参数。y的确切值将取决于数据范围。
labelled.bars.v2 <- bars &#43;geom_label(aes(x &#61; country, y &#61; 4, label &#61; round(lifeExp, 0)),hjust &#61; 0, vjust &#61; 0.5, colour &#61; "white", fill &#61; NA, label.size &#61; NA, family&#61;"Helvetica", size &#61; 6)labelled.bars.v2
通过geom_segment函数
multiple_line &#43; geom_segment(aes(x &#61; 1979, y &#61; 45, xend &#61; 1965, yend &#61; 43), colour &#61; "#555555", size&#61;0.5)
size
函数设置了线条的宽度。
geom_curve
函数
multiple_line &#43; geom_curve(aes(x &#61; 1979, y &#61; 45, xend &#61; 1965, yend &#61; 43), colour &#61; "#555555", curvature &#61; -0.2,size&#61;0.5)
&#96;&#96;曲率&#39;&#39;参数设置曲线的数量&#xff1a;0为直线&#xff0c;负值给出左手曲线&#xff0c;正值给出右手曲线。
仅仅需要增加一个arrow
参数。
multiple_line &#43; geom_curve(aes(x &#61; 1979, y &#61; 45, xend &#61; 1965, yend &#61; 43), colour &#61; "#555555", size&#61;0.5, curvature &#61; -0.2,arrow &#61; arrow(length &#61; unit(0.03, "npc")))
image.png
在整个图上添加一条线的最简单方法是使用geom_vline&#xff08;&#xff09;
表示垂直线&#xff0c;或者geom_hline&#xff08;&#xff09;
表示水平线。可选的其他参数可以指定线条的大小&#xff0c;颜色和类型&#xff08;默认选项为实线&#xff09;。
multiple_line &#43; geom_hline(yintercept &#61; 10, size &#61; 1, colour &#61; "red", linetype &#61; "dashed")
在此示例对想突出显示某些内容&#xff08;例如&#xff0c; 阈值水平或平均值&#xff09;特别有用。
&#43; geom_hline(yintercept &#61; 0, size &#61; 1, colour &#61; "#333333")
如果想将可视化的数据按某个变量进行拆分&#xff0c;则需要使用facet_wrap或facet_grid。将要除以的变量添加到以下代码行&#xff1a;facet_wrap&#xff08;〜变量&#xff09;&#xff0c;分面换行的另一个参数ncol
指定列数&#xff1a;
#Prepare data
facet <- gapminder %>%filter(continent !&#61; "Americas") %>%group_by(continent, year) %>%summarise(pop &#61; sum(as.numeric(pop)))#Make plot
facet_plot <- ggplot() &#43;geom_area(data &#61; facet, aes(x &#61; year, y &#61; pop, fill &#61; continent)) &#43;scale_fill_manual(values &#61; c("#FAAB18", "#1380A1","#990000", "#588300")) &#43; facet_wrap( ~ continent, ncol &#61; 5) &#43; scale_y_continuous(breaks &#61; c(0, 2000000000, 4000000000),labels &#61; c(0, "2bn", "4bn")) &#43;bbc_style() &#43;geom_hline(yintercept &#61; 0, size &#61; 1, colour &#61; "#333333") &#43;theme(legend.position &#61; "none",axis.text.x &#61; element_blank()) &#43;labs(title &#61; "Asia&#39;s rapid growth",subtitle &#61; "Population growth by continent, 1952-2007")
可能已经在上表中注意到&#xff0c;人口相对较少的大洋洲已经完全消失。默认情况下&#xff0c;构面在较小的倍数上使用固定的轴比例。 最好始终在较小的倍数上使用相同的y轴比例&#xff0c;以避免产生误导&#xff0c;但有时您可能需要为每个倍数分别设置这些值&#xff0c;我们可以通过添加参数scales &#61;“ free”来实现。如果只想释放一个轴的刻度&#xff0c;则将参数设置为free_x
或free_y
。
#Make plot
facet_plot_free <- ggplot() &#43;geom_area(data &#61; facet, aes(x &#61; year, y &#61; pop, fill &#61; continent)) &#43;facet_wrap(~ continent, scales &#61; "free") &#43; bbc_style() &#43;scale_fill_manual(values &#61; c("#FAAB18", "#1380A1","#990000", "#588300")) &#43;geom_hline(yintercept &#61; 0, size &#61; 1, colour &#61; "#333333") &#43;theme(legend.position &#61; "none",axis.text.x &#61; element_blank(),axis.text.y &#61; element_blank()) &#43;labs(title &#61; "It&#39;s all relative",subtitle &#61; "Relative population growth by continent,1952-2007")
image.png
您可以更改情节几乎所有元素&#xff08;标题&#xff0c;字幕&#xff0c;图例&#xff09;或情节本身周围的边距。
通常&#xff0c;您不需要更改主题的默认边距&#xff0c;但是如果这样做&#xff0c;则语法为theme&#xff08;ELEMENT &#61; element_text&#xff08;margin &#61; margin&#xff08;0&#xff0c;5&#xff0c;10&#xff0c;0&#xff09;&#xff09;&#xff09;
。
这些数字分别指定顶部&#xff0c;右侧&#xff0c;底部和左侧的边距-但您也可以直接指定要更改的边距。 例如&#xff0c;让我们尝试为字幕提供一个较大的底边距&#xff1a;
bars &#43; theme(plot.subtitle&#61;element_text(margin&#61;margin(b&#61;75)))
当您制作的绘图超出bbplot中的默认高度450px时&#xff0c;您确实需要考虑x轴边距的大小。 例如&#xff0c;如果要创建带有很多条形图的条形图&#xff0c;并要确保每个条形图和标签之间有一定的呼吸空间&#xff0c;则可能是这种情况。 如果您确实保留了较大高度图的边距&#xff0c;那么轴和标签之间的间隙可能会更大。
这是我们在处理条形图的边距和高度时应用的指南&#xff08;已应用coord_flip&#xff09;
sizetb550px510650px710750px1010850px1410
因此&#xff0c;您需要做的就是将此代码添加到图表中&#xff0c;例如&#xff0c;如果您希望绘图的高度为650px而不是450px。
bar_chart_tall <- bars &#43; theme(axis.text.x &#61; element_text(margin&#61;margin(t &#61; 7, b &#61; 10)))
bar_chart_tall
image.png
尽管可能性要小得多&#xff0c;但是如果您确实想对折线图进行等效处理并以大于默认高度的高度导出它&#xff0c;则需要执行相同的操作&#xff0c;但是要根据上表将t的值更改为负值 。
默认情况下&#xff0c;R将按字母顺序显示数据&#xff0c;但按大小排列则很简单&#xff1a;只需将reorder&#xff08;&#xff09;
包装在要重新排列的x
或y
变量周围&#xff0c;然后指定要变量 重新排序。
例如。 x &#61;重新排序&#xff08;国家/地区&#xff0c;流行&#xff09;。 升序是默认设置&#xff0c;但是您可以通过将desc&#xff08;&#xff09;
包裹在要排序的变量周围来将其更改为降序&#xff1a;
bars <- ggplot(bar_df, aes(x &#61; reorder(country, lifeExp), y &#61; lifeExp)) &#43;geom_bar(stat&#61;"identity", position&#61;"identity", fill&#61;"#1380A1") &#43;geom_hline(yintercept &#61; 0, size &#61; 1, colour&#61;"#333333") &#43;bbc_style() &#43;coord_flip() &#43;labs(title&#61;"Reunion is highest",subtitle &#61; "Highest African life expectancy, 2007") &#43;theme(panel.grid.major.x &#61; element_line(color&#61;"#cbcbcb"), panel.grid.major.y&#61;element_blank())
image.png
有时&#xff0c;您需要以不按字母顺序或按大小重新排序的方式对数据进行排序。为了正确排序这些参数&#xff0c;需要在绘制图表之前设置数据的因子水平&#xff0c;在levels
参数中指定要绘制类别的顺序&#xff1a;
dataset$column <- factor(dataset$column, levels &#61; c("18-24","25-64","65&#43;"))
可以通过 ifelse()
设置图片的 fill, alpha, size。
fill&#61;ifelse(logical_condition, fill_if_true, fill_if_false)
ggplot(bar_df, aes(x &#61; reorder(country, lifeExp), y &#61; lifeExp)) &#43;geom_bar(stat&#61;"identity", position&#61;"identity", fill&#61;ifelse(bar_df$country &#61;&#61; "Mauritius", "#1380A1", "#dddddd")) &#43;geom_hline(yintercept &#61; 0, size &#61; 1, colour&#61;"#333333") &#43;bbc_style() &#43;coord_flip() &#43;labs(title&#61;"Reunion is highest",subtitle &#61; "Highest African life expectancy, 2007") &#43;theme(panel.grid.major.x &#61; element_line(color&#61;"#cbcbcb"), panel.grid.major.y&#61;element_blank())
4人点赞
R语言第四章可视化