小米盒子4 拆解图解
Customizing a graph to transform it into a beautiful figure in R isn’t alchemy. Nonetheless, it took me a lot of time (and frustration) to figure out how to make these plots informative and publication-quality. Rather than hoarding this information like a dragon presiding over its treasure, I want to share the code I use to plot data, explaining what each piece of it does.
自定义图形以将其转换为R中的漂亮图形并不是炼金术。 尽管如此,我还是花了很多时间(和沮丧)来弄清楚如何使这些情节具有信息性和出版质量。 我要分享的是我用来绘制数据的代码,而不是像巨龙一样掌控着它的宝藏,而是解释了每个信息的作用。
First thing’s first, let’s load our packages and generate a dummy dataset, with one independent variable and one continuous dependent variable. Before the code snippet, I’ll discuss the functions and arguments we will use. We also set an order for our categorical variables beforehand.
首先,让我们加载包并生成一个虚拟数据集,其中包含一个自变量和一个连续因变量。 在代码片段之前,我将讨论我们将使用的函数和参数。 我们还预先为分类变量设置了顺序。
虚拟数据集:函数和参数 (Dummy Dataset: Functions and arguments)
runif generates 100 numbers between 0 and 1
runif生成100个介于0和1之间的数字
sample randomly selects the number 1 or 2, 100 times
样本随机选择数字1或2,进行100次
factor sets a preferred order for our grouping variables
因子为分组变量设置了首选顺序
### Load packages
install.packages('ggplot2')
install.packages('RColorBrewer')
install.packages('ggpubr')
library(ggplot2)
library(RColorBrewer)
library(ggpubr)#Create dummy dataset
ndf <- data.frame(
value = rep(runif(100)),
factor1 = as.char(rep(sample(1:2, replace = TRUE, 100))
)
ndf$factor1 = factor(ndf$factor1, levels = c('1', '2'))
箱形图:函数和参数 (Box Plot: Functions and Arguments)
ggplot allows us to specify the independent and dependent variables, as well as the dataset to use for the graph
ggplot允许我们指定自变量和因变量,以及用于图的数据集
stat_boxplot lets us specify the type of whiskers to add onto the plot
stat_boxplot让我们指定要添加到绘图中的晶须类型
geom_boxplot specifies the independent and dependent variables for the boxes in the plot
geom_boxplot为图中的框指定自变量和因变量
The first basic attempt isn’t very informative or visually appealing. We focus first on just plotting the first independent variable, factor1. I also don’t like the default grey theme within ggplot.
最初的基本尝试不是很有启发性或视觉吸引力。 我们首先专注于绘制第一个自变量factor1。 我也不喜欢ggplot中的默认灰色主题。
plot_data <- ggplot(ndf, aes(y = value, x = factor1)) +
stat_boxplot( aes(y = value, x = factor1 ),
geom='errorbar', width=0.5) +
geom_boxplot(aes(y = value, x = factor1))
带点的箱形图:函数和参数 (Box Plot with Dots: Functions and Arguments)
theme_bw() sets a black-and-white theme for the plot, getting rid of the pesky grey background
theme_bw()为情节设置了黑白主题,摆脱了讨厌的灰色背景
geom_dotplot: binaxis specifies which variable will be displayed with the dots. The other arguments specify the appearance of the dots, while binwidth specifies how many dots we want in the same row on our plot. If you want to make the dots see-through, use the alpha argument which lets you specify the opacity of the dots from 0 to 1.
geom_dotplot: binaxis指定将与点一起显示的变量。 其他参数指定点的外观,而binwidth指定在绘图的同一行中需要多少个点。 如果要使点透明,请使用alpha参数,该参数可以指定点的不透明度从0到1。
Now our plot is more informative but it still needs improvement. We want to modify some of the colors, axes and labels.
现在我们的情节提供了更多信息,但仍需要改进。 我们要修改一些颜色,轴和标签。
plot_data <- ggplot(ndf, aes(y = value, x = factor1)) +
stat_boxplot( aes(y = value, x = factor1 ),
geom='errorbar', width = 0.5) +
geom_boxplot(aes(y = value, x = factor1)) +
theme_bw() +
geom_dotplot(binaxis='y',
stackdir = "center",
binwidth = 1/20,
dotsize = 1,
aes(fill = factor1))
出汗的小事情 (Sweating the Little Things)
scale_fill_manual lets us set specific colors for specific values of our independent variables
scale_fill_manual让我们为自变量的特定值设置特定的颜色
xlab and ylab allow us to label the x and y axis respectively. If you put the escape character following the newline symbol into the title (\n) it continues the rest of the label on the next line!
xlab和ylab允许我们分别标记x和y轴。 如果将换行符后面的转义字符放在标题(\ n)中,它将在下一行继续其余标签!
ggtitle names your plot
ggtitle为您的情节命名
theme is a very customizable part of the script. It let’s us customize just about anything we want with its arguments. Since I don’t like the default grids placed in ggplots, I attribute element_blank to a few panel arguments to get rid of the border around the plot. I make the axis colored black rather than grey. I also specify how much blank space I want around the graph with plot_margin. Since this plot is simple, we get rid of the legend for this example. With the various axis_text arguments I set the size, font face, color and positioning of axis text and labels.
主题是脚本中非常可定制的部分。 它使我们可以自定义几乎所有我们想要的参数。 由于我不喜欢放置在ggplots中的默认网格,因此我将element_blank赋予一些面板参数以摆脱绘图周围的边界。 我将轴设为黑色而不是灰色。 我还使用plot_margin指定想要在图形周围留多少空白。 由于该图很简单,因此在此示例中,我们摆脱了图例。 通过各种axis_text参数,我设置了轴文本和标签的大小,字体,颜色和位置。
scale_y_continuous sets the y axis increases from 0 to 1 in 0.2 intervals. By expanding the limit of the plot beyond 1, it gives the title more breathing room. If I needed to use a y-axis with discrete variable names, I could use a different argument — scale_y_discrete.
scale_y_continuous设置y轴以0.2的间隔从0增加到1。 通过将图的限制扩展到超过1,可以为标题提供更多的喘息空间。 如果我需要使用带有离散变量名称的y轴,则可以使用其他参数-scale_y_discrete。
scale_x_discrete lets me relabel the two independent variables. This lets you quickly rename codified data. I’ve relabeled the independent variables as Danny DeVito and Nicolas Cage. Let’s posit that we are looking at how well Nic Cage and Danny DeVito performed at the box office using scores scaled between 0 and 1.
scale_x_discrete让我重新标记两个自变量。 这使您可以快速重命名编码的数据。 我将自变量重新标记为Danny DeVito和Nicolas Cage。 假设我们正在研究Nic Cage和Danny DeVito在0和1之间的得分在票房上的表现。
stat_compare_means lets you do statistical tests. This one’s here for you to explore! Just type stat_compare_means( and press tab to explore the different options available to you. Otherwise you can type ?stat_compare_means to get the help manual for this function!
stat_compare_means使您可以进行统计检验。 这个在这里供您探索! 只需键入stat_compare_means(并按Tab键即可探索可用的其他选项。否则,您可以键入?stat_compare_means以获得此功能的帮助手册!
If you’re wondering about the hjust and vjust within different elements of the code, it simply allows us to change position of text elements. I added some flare by changing the boxplot line coloring as well as the fill of our dots. If you’re building a busy plot, it’s also important to check for accessibility. You can find colorblind friendly palette with this tool: https://colorbrewer2.org/.
如果您想知道代码中不同元素之间的平衡,它只是允许我们更改文本元素的位置。 我通过更改箱形图线的颜色以及点的填充来增加了耀斑。 如果您要建立繁忙的地块,检查可访问性也很重要。 您可以使用此工具找到对色盲友好的调色板: https : //colorbrewer2.org/ 。
For convenience, I’ve bolded any changes in the code for the plot to make it easier to follow.
为了方便起见,我将图中代码的所有更改加粗了,以使其易于理解。
plot_data <- ggplot(ndf,
aes(y = value, x = factor1, color = factor1))+
scale_color_manual(values =
c('1' = 'blue',
'2' = 'firebrick4')) +
stat_boxplot( aes(y = value, x = factor1 ),
geom='errorbar', width = 0.5) +
geom_boxplot(aes(y = value, x = factor1), shape = 2)+
theme_bw() +
geom_dotplot(binaxis='y',
stackdir = "center",
binwidth = 1/20,
dotsize = 1,
aes(fill = factor1)) +
scale_fill_manual(values = c('1' = '#0571b0', '2' = '#ca0020')) +
xlab("Independent\nVariable") + ylab("Dependent\nVariable") +
ggtitle("Box-Dot Plot") +
theme( panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"),
panel.border = element_blank(),
plot.margin=unit(c(0.5,0.5, 0.5,0.5),"cm"),
legend.position="none",
axis.title.x= element_text(size=14, color = 'black', vjust = 0.5),
axis.title.y= element_text(size=14, color = 'black', vjust = 0.5),
axis.text.x = element_text(size=12, color = 'black'),
axis.text.y = element_text(size=14, color = 'black'),
plot.title = element_text(hjust = 0.5, size=16, face = 'bold', vjust = 0.5)) +
scale_y_continuous(expand = c(0,0), breaks = seq(0,1,0.2),
limits = c(0,1.1)) +
scale_x_discrete(labels = c('Nicholas Cage', 'Danny DeVito')) stat_compare_means(method = 't.test', hide.ns = FALSE, size = 4, vjust = 2)
In a very short span of time, we’ve gone from a simple boxplot to a more visually appealing, accessible plot that gives us more information about the individual datapoints. You can save the code and modify it as you wish whenever you need to plot data. This makes the customization process so much easier! No more (okay maybe a lot less) computer defenestrations!
在很短的时间内,我们已经从简单的箱形图变成了更具视觉吸引力的可访问图,它为我们提供了有关各个数据点的更多信息。 您可以在需要绘制数据时保存并根据需要修改代码。 这使定制过程变得非常容易! 没有更多(好的,也许更少)计算机故障!
If you’re looking to download the code, find it here: https://github.com/simon-sp/R_ggplot_basic_dotplot. I’ve left plenty of comments within the code to help you out in case you get stuck! Remember that placing a question mark before a function brings up an instruction guide for you in RStudio!
如果您要下载代码,请在这里找到它: https : //github.com/simon-sp/R_ggplot_basic_dotplot 。 我在代码中留下了很多注释,以帮助您避免被卡住! 请记住,在功能之前加一个问号会在RStudio中为您提供指导指南!
翻译自: https://towardsdatascience.com/the-box-plot-guide-i-wish-i-had-when-i-started-learning-r-d1e9705a6a37
小米盒子4 拆解图解
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388259.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!