龙空技术网

ggplot2不仅是R语言中最流行的数据可视化包,它也是一个生态系统

数字化转型网官方 52

前言:

当前各位老铁们对“可视化包”大约比较着重,你们都想要知道一些“可视化包”的相关资讯。那么小编在网摘上汇集了一些有关“可视化包””的相关资讯,希望同学们能喜欢,兄弟们快快来学习一下吧!

ggplot2不仅是R语言中最流行的数据可视化包,它也是一个生态系统。大量的附加包为ggplot提供了更强大的功能,从更容易地更改轴标签到自动生成统计信息,再到自定义……几乎任何事情。

下面是您应该了解的12个很棒的ggplot2扩展——最后还有一些额外的扩展。

一、创建自己的geoms: ggpackets

一旦您添加了多个层并对ggplot图形进行了调整,您如何保存这些工作以便易于重用呢?一种方法是将代码转换为函数。另一种方法是将其转换为RStudio代码片段。但是ggpackets包有一种对ggplot更友好的方式:创建您自己的自定义geom!这就像使用ggpacket()函数将其存储在变量中一样简单。

下面的示例代码根据波士顿降雪数据创建了一个条形图,其中有几行自定义代码,我想在其他数据中再次使用它们。第一个代码块是初始图:

library(ggplot2)

library(scales)

library(rio)

snowfall2000s <- import(“”)

ggplot(snowfall2000s, aes(x = Winter, y = Total)) +

geom_col(color = “black”, fill=”#0072B2″) +

theme_minimal() +

theme(panel.border = element_blank(), panel.grid.major = element_blank(),

panel.grid.minor = element_blank(), axis.line =

element_line(colour = “gray”),

plot.title = element_text(hjust = 0.5),

plot.subtitle = element_text(hjust = 0.5)

) +

ylab(“”) + xlab(“”)

下面是如何把它变成一个名为my_geom_col的自定义geom:

library(ggpackets)

my_geom_col <- ggpacket() +

geom_col(color = “black”, fill=”#0072B2″) +

theme_minimal() +

theme(panel.border = element_blank(), panel.grid.major = element_blank(),

panel.grid.minor = element_blank(), axis.line =

element_line(colour = “gray”),

plot.title = element_text(hjust = 0.5),

plot.subtitle = element_text(hjust = 0.5)

) +

ylab(“”) + xlab(“”)

注意,除了原始图形的第一行ggplot()代码外,我将所有内容都保存到自定义geom中。

下面是使用新geom的简单方法:

ggplot(snowfall2000s, aes(x = Winter, y = Total)) +

my_geom_col()

使用自定义ggpackets geom创建的图形。

ggpackets由Doug Kelkhoff编写,可在CRAN上使用。

二、更简单的ggplot2代码:ggblanket和其他

Ggplot2非常强大且可定制,但有时这是以复杂性为代价的。有几个包旨在简化ggplot2,使常见的数据可视化更简单或更直观。

如果你倾向于忘记用哪个gem来做什么,我建议你试试ggblanket。我最喜欢这个包的一点是,它将col和fill美学合并为一个col美学,所以我不再需要记住是使用scale_fill_还是scale_colour_函数。

ggblanket的另一个好处是:gg_col()或gg_point()等gem在函数本身中包含自定义选项,而不需要单独的层。这意味着我只需要查看一个帮助文件就可以看到诸如pal用于定义调色板和y_title设置y轴标题之类的东西,而不是为多个单独的函数搜索帮助文件。Ggblanket可能不会让我更容易记住所有这些选项,但它们更容易找到。华东CIO大会、华东CIO联盟、CDLC中国数字化灯塔大会、CXO数字化研学之旅、数字化江湖-讲武堂,数字化江湖-大侠传、数字化江湖-论剑、CXO系列管理论坛(陆家嘴CXO管理论坛、宁波东钱湖CXO管理论坛等)、数字化转型网,走进灯塔工厂系列、ECIO大会等

下面是如何使用ggblanket从帕尔默企鹅数据集中生成直方图(示例取自软件包网站):

library(ggblanket)

library(palmerpenguins)

penguins |>

gg_histogram(x = body_mass_g, col = species)

使用ggblanket创建的直方图。

结果仍然是一个ggplot对象,这意味着您可以通过使用传统的ggplot2代码添加层来继续定制它。

ggblanket由David Hodge编写,可在CRAN上下载。

其他几个包也尝试简化ggplot2并更改其默认值,包括ggcharts。它的简化函数使用语法

library(ggcharts)

column_chart(snowfall2000s, x = Winter, y = Total)

这一行代码提供了一个相当不错的默认值,加上自动排序的条(您可以轻松地重写它)。

用ggcharts创建的柱状图自动按值对柱状图进行排序。

请参阅InfoWorld ggcharts教程或下面的视频了解更多详细信息。

简单的文本定制:ggeasy

Ggeasy不会影响数据可视化的“主要”部分,即条/点/行大小、颜色、顺序等。相反,它完全是关于围绕图定制文本,比如标签和轴格式。所有ggeasy函数都以easy_开头,所以使用RStudio自动补全很容易找到它们。

需要将情节标题置于中心位置?easy_center_title()。想要将x轴标签旋转90度?Easy_rotate_labels (which = “x”)。

在InfoWorld ggeasy教程或下面的视频中了解更多关于这个包的信息。

ggeasy由Jonathan Carroll和其他人编写,可在CRAN上使用。

突出显示图中的项目:gghighlight

有时你想要注意图表中的特定数据点。您当然可以单独使用ggplot来实现这一点,但是gghighlight的目的是使它更简单。只需添加gghighlight()函数和一个条件。例如,如果总降雪量高于85英寸的冬季对我正在讲述的故事很重要,我可以使用gghighlight(total > 85):

library(gghighlight)

ggplot(snowfall2000s, aes(x = Winter, y = Total)) +

my_geom_col() +

gghighlight(Total > 85)

使用gghighlight突出显示总数超过85的图形。

或者如果我想要调用特定的年份,例如2011-12和2014-15,我可以将它们设置为gghighlight()条件:

ggplot(snowfall2000s, aes(x = Winter, y = Total)) +

my_geom_col() +

gghighlight(Winter %in% c(‘2011-12’, ‘2014-15’))

gghighlight由Hiroaki Yutani撰写,可在CRAN上下载。

添加主题或调色板:ggthemes和其他

ggplot2生态系统包括许多用于添加主题和调色板的包。你可能不需要全部,但你可能想要浏览它们,找到那些你觉得有吸引力的主题或调色板。

在安装其中一个包之后,通常可以像使用内置的ggplot2主题或调色板一样使用新的主题或调色板。下面是ggthemes包的太阳主题和色盲调色板的示例:

library(ggthemes)

ggplot(penguins, aes(x = bill_length_mm, y = body_mass_g, color = species)) +

geom_point() +

ggthemes::theme_solarized() +

scale_color_colorblind()

散点图使用色盲调色板和太阳主题从ggthemes包。

ggthemes由Jeffrey B. Arnold等人编写,可在CRAN上下载。

其他需要考虑的主题和调色板包:

ggsci是ggplot2调色板的集合,“灵感来自科学期刊、数据可视化库、科幻电影和电视节目”,如scale_fill_lancet()和scale_color_startrek()。

Hrbrthemes是一个流行的主题包,专注于排版。

ggthemr不像其他人那么有名,但它有很多主题可供选择,加上一个GitHub回购,可以很容易地浏览主题并看到它们的样子。

bbplot只有一个主题bbc_style(),即BBC的发布风格,以及第二个保存用于发布的plot的函数finalise_plot()。

paletteer是一个元包,将来自几十个单独的R调色板包的调色板组合成一个具有单一一致接口的调色板包。该接口包括专门用于ggplot的函数,其语法为scale_color_paletteer_d(“nord::aurora”)。这里nord是原始的调色板包名称,aurora是特定的调色板名称,_d表示该调色板用于离散值(非连续值)。一开始,调色板可能有点让人不知所措,但你几乎肯定会找到一个吸引你的调色板。

请注意,您可以使用ggplot的任何R调色板,即使它没有特定于ggplot的颜色缩放函数,也可以使用ggplot的手动缩放函数和调色板值,例如scale_color_manual(values=c(“#486030″, “#c03018″, “#f0a800”))。三、向ggplot2 text: ggtext添加颜色和其他样式

ggtext包使用类似标记的语法向图形中的文本添加样式和颜色。例如,文本周围的下划线添加斜体,文本周围的两个星号创建粗体样式。为了使它与ggtext一起正常工作,包的element_markdown()函数也必须添加到ggplot主题中。语法是将适当的markdown样式添加到文本中,然后将element_markdown()添加到主题的元素中,例如用于斜体副标题:

library(ggtext)

ggplot(snowfall2000s, aes(x = Winter, y = Total)) +

my_geom_col() +

labs(title = “Annual Boston Snowfall”, subtitle = “_2000 to 2016_”) +

theme(

plot.subtitle = element_markdown()

)

本文作者Claus O. Wilke,可在CRAN上下载。

传递不确定性:ggdist

Ggdist添加了用于可视化数据分布和不确定性的几何体,使用stat_slab()和stat_dotsinterval()等新的几何体生成雨云图和logit点图等图形。以下是ggdist网站上的一个例子:

使用ggdist包生成雨云图。

请访问ggdist网站了解详细信息和更多示例。ggidst由Matthew Kay编写,可在CRAN上下载。

四、向ggplot2添加交互性:plotly和ggiraph

如果你的图表在网上,你可能希望它们是交互式的,提供像关闭和打开系列以及当鼠标在点、线或条上时显示底层数据等功能。plotly和ggiraph都将ggplot转换为交互式HTML小部件。

plotly是plotly.js JavaScript库的一个R包装器,使用起来非常简单。您所要做的就是将最终的ggplot放置在包的ggplotly()函数中,该函数将返回绘图的交互式版本。例如:

library(plotly)

ggplotly(

ggplot(snowfall2000s, aes(x = Winter, y = Total)) +

geom_col() +

labs(title = “Annual Boston Snowfall”, subtitle = “2000 to 2016”)

)

Plotly与其他扩展一起工作,包括ggpackets和gghighlights。plot图并不总是包含静态版本中出现的所有内容(例如,在撰写本文时,它还不能识别ggplot2字幕)。但在快速交互性方面,这个软件包很难被超越。

注意,plotly库还有一个与ggplot无关的函数plot_ly(),它使用的语法类似于ggplot的qplot():

plot_ly(snowfall2000s, x = ~Winter, y = ~Total, type = “bar”)

原文:

ggplot2 is not only the R language’s most popular data visualization package, it is also an ecosystem. Numerous add-on packages give ggplot added power to do everything from more easily changing axis labels to auto-generating statistical information to customizing . . . almost anything.

Here are a dozen great ggplot2 extensions you should know — plus some more extras at the end.

TABLE OF CONTENTS

Create your own geoms: ggpacketsEasier ggplot2 code: ggblanket and othersSimple text customization: ggeasyHighlight items in your plots: gghighlightAdd themes or color palettes: ggthemes and others

SHOW MORE

Create your own geoms: ggpackets

Once you’ve added multiple layers and tweaks to a ggplot graph, how can you save that work so it’s easy to re-use? One way is to convert your code into a function. Another is to turn it into an RStudio code snippet. But the ggpackets package has a ggplot-friendlier way: Create your own custom geom! It’s as painless as storing it in a variable using the ggpacket() function.

The example code below creates a bar chart from Boston snowfall data, and it has several lines of customizations that I’d like to use again with other data. The first code block is the initial graph:

library(ggplot2)

library(scales)

library(rio)

snowfall2000s <- import(“”)

ggplot(snowfall2000s, aes(x = Winter, y = Total)) +

geom_col(color = “black”, fill=”#0072B2″) +

theme_minimal() +

theme(panel.border = element_blank(), panel.grid.major = element_blank(),

panel.grid.minor = element_blank(), axis.line =

element_line(colour = “gray”),

plot.title = element_text(hjust = 0.5),

plot.subtitle = element_text(hjust = 0.5)

) +

ylab(“”) + xlab(“”)

Here’s how to turn that into a custom geom called my_geom_col:

library(ggpackets)

my_geom_col <- ggpacket() +

geom_col(color = “black”, fill=”#0072B2″) +

theme_minimal() +

theme(panel.border = element_blank(), panel.grid.major = element_blank(),

panel.grid.minor = element_blank(), axis.line =

element_line(colour = “gray”),

plot.title = element_text(hjust = 0.5),

plot.subtitle = element_text(hjust = 0.5)

) +

ylab(“”) + xlab(“”)

Note that I saved everything except the original graph’s first ggplot() line of code to the custom geom.

Here’s how simple it is to use that new geom:

ggplot(snowfall2000s, aes(x = Winter, y = Total)) +

my_geom_col()

Sharon Machlis

Graph created with a custom ggpackets geom.

ggpackets is by Doug Kelkhoff and is available on CRAN.

Easier ggplot2 code: ggblanket and others

ggplot2 is incredibly powerful and customizable, but sometimes that comes at a cost of complexity. Several packages aim to streamline ggplot2 so common data visualizations are either simpler or more intuitive.

If you tend to forget which geoms to use for what, I recommend giving ggblanket a try. One of my favorite things about the package is that it merges col and fill aesthetics into a single col aesthetic, so I no longer need to remember whether to use a scale_fill_ or scale_colour_ function.

Another ggblanket benefit: Its geoms such as gg_col() or gg_point() include customization options within the functions themselves instead of requiring separate layers. And that means I only need to look at one help file to see things like pal is for defining a color palette and y_title sets the y-axis title, instead of searching help files for multiple separate functions. ggblanket may not make it easier for me to remember all those options, but they are easier to find.

Here’s how to generate a histogram from the Palmer penguins data set with ggblanket, (example taken from the package website):

library(ggblanket)

library(palmerpenguins)

penguins |>

gg_histogram(x = body_mass_g, col = species)

Sharon Machlis

Histogram created with ggblanket.

The result is still a ggplot object, which means you can continue customizing it by adding layers with conventional ggplot2 code.

ggblanket is by David Hodge and is available on CRAN.

Several other packages try to simplify ggplot2 and change its defaults, too, including ggcharts. Its simplified functions use syntax like

library(ggcharts)

column_chart(snowfall2000s, x = Winter, y = Total)

That single line of code provides a pretty decent default, plus automatically sorted bars (you can easily override that).

Sharon Machlis

Bar chart created with ggcharts automatically sorts the bars by values.

See the InfoWorld ggcharts tutorial or the video below for more details.

Simple text customization: ggeasy

ggeasy doesn’t affect the “main” part of your dataviz—that is, the bar/point/line sizes, colors, orders, and so on. Instead, it’s all about customizing the text around the plots, such as labels and axis formatting. All ggeasy functions start with easy_ so it’s, yes, easy to find them using RStudio auto-complete.

Need to center a plot title? easy_center_title(). Want to rotate x-axis labels 90 degrees? easy_rotate_labels(which = “x”).

Learn more about the package in the InfoWorld ggeasy tutorial or the video below.

ggeasy is by Jonathan Carroll and others and is available on CRAN.

Highlight items in your plots: gghighlight

Sometimes you want to call attention to specific data points in a graph. You can certainly do that with ggplot alone, but gghighlight aims to make it easier. Just add the gghighlight() function along with a condition. For example, if winters with total snowfall higher than 85 inches are important to the story I’m telling, I could use gghighlight(Total > 85):

library(gghighlight)

ggplot(snowfall2000s, aes(x = Winter, y = Total)) +

my_geom_col() +

gghighlight(Total > 85)

Sharon Machlis

Graph with totals over 85 highlighted using gghighliight.

Or if I want to call out specific years, such as 2011-12 and 2014-15, I can set those as my gghighlight() condition:

ggplot(snowfall2000s, aes(x = Winter, y = Total)) +

my_geom_col() +

gghighlight(Winter %in% c(‘2011-12’, ‘2014-15’))

gghighlight is by Hiroaki Yutani and is available on CRAN.

Add themes or color palettes: ggthemes and others

The ggplot2 ecosystem includes a number of packages to add themes and color palettes. You likely won’t need them all, but you may want to browse through them to find ones that have themes or palettes you find compelling.

After installing one of these packages, you can usually use a new theme or color palette in the same way that you’d use a built-in ggplot2 theme or palette. Here’s an example with the ggthemes package’s solarized theme and colorblind palette:

library(ggthemes)

ggplot(penguins, aes(x = bill_length_mm, y = body_mass_g, color = species)) +

geom_point() +

ggthemes::theme_solarized() +

scale_color_colorblind()

Sharon Machlis

Scatter plot using a colorblind palette and solarized theme from the ggthemes package.

ggthemes is by Jeffrey B. Arnold and others and is available on CRAN.

Other theme and palette packages to consider:

ggsci is a collection of ggplot2 color palettes “inspired by scientific journals, data visualization libraries, science fiction movies, and TV shows” such as scale_fill_lancet() and scale_color_startrek().

hrbrthemes is a popular theme package with a focus on typography.

ggthemr is a bit less well known than those others, but it has a lot of themes to choose from plus a GitHub repo that makes it easy to browse themes and see what they look like.

bbplot has just a single theme, bbc_style(), the publication-ready style of the BBC, as well as a second function to save a plot for publication, finalise_plot().

paletteer is a meta package, combining palettes from dozens of separate R palette packages into one with a single consistent interface. And that interface includes functions specifically for ggplot use, with a syntax such as scale_color_paletteer_d(“nord::aurora”). Here nord is the original palette package name, aurora is the specific palette name, and the _d signifies that this palette is for discreet values (not continuous). paletteer can be a little overwhelming at first, but you will almost certainly find a palette that appeals to you.

Note that you can use any R color palette with ggplot, even if it doesn’t have ggplot-specific color scale functions, with ggplot’s manual scale functions and the color palette values, such as scale_color_manual(values=c(“#486030”, “#c03018”, “#f0a800”)).

Add color and other styling to ggplot2 text: ggtext

The ggtext package uses markdown-like syntax to add styles and colors to text within a plot. For example, underscores surrounding the text add italics and two asterisks around the text create bold styling. For this to work properly with ggtext, the package’s element_markdown() function must be added to a ggplot theme, too. Syntax is to add the appropriate markdown styling to the text and then add element_markdown() to the element of the theme, such as this for italicizing a subtitle:

library(ggtext)

ggplot(snowfall2000s, aes(x = Winter, y = Total)) +

my_geom_col() +

labs(title = “Annual Boston Snowfall”, subtitle = “_2000 to 2016_”) +

theme(

plot.subtitle = element_markdown()

)

ggtext is by Claus O. Wilke and is available on CRAN.

Convey uncertainty: ggdist

ggdist adds geoms for visualizing data distribution and uncertainty, generating graphics like rain cloud plots and logit dotplots with new geoms like stat_slab() and stat_dotsinterval(). Here’s one example from the ggdist website:

library(ggdist)

set.seed(12345) # for reproducibility

data.frame(

abc = c(“a”, “b”, “b”, “c”),

value = rnorm(200, c(1, 8, 8, 3), c(1, 1.5, 1.5, 1))

) %>%

ggplot(aes(y = abc, x = value, fill = abc)) +

stat_slab(aes(thickness = stat(pdf*n)), scale = 0.7) +

stat_dotsinterval(side = “bottom”, scale = 0.7, slab_size = NA) +

scale_fill_brewer(palette = “Set2”)

Sharon Machlis

Rain cloud plot generated with the ggdist package.

Check out the ggdist website for full details and more examples. ggidst is by Matthew Kay and is available on CRAN.

Add interactivity to ggplot2: plotly and ggiraph

If your plots are going on the web, you might want them to be interactive, offering features like turning series off and on and displaying underlying data when mousing over a point, line, or bar. Both plotly and ggiraph turn ggplots into interactive HTML widgets.

plotly, an R wrapper to the plotly.js JavaScript library, is extremely simple to use. All you do is place your final ggplot within the package’s ggplotly() function, and the function returns an interactive version of your plot. For example:

library(plotly)

ggplotly(

ggplot(snowfall2000s, aes(x = Winter, y = Total)) +

geom_col() +

labs(title = “Annual Boston Snowfall”, subtitle = “2000 to 2016”)

)

plotly works with other extensions, including ggpackets and gghighlights. plotly graphs don’t always include everything that appears in a static version (as of this writing it didn’t recognize ggplot2 subtitles, for example). But the package is hard to beat for quick interactivity.

Note that the plotly library also has a non-ggplot-related function, plot_ly(), which uses a syntax similar to ggplot’s qplot():

plot_ly(snowfall2000s, x = ~Winter, y = ~Total, type = “bar”)

CXO联盟(CXO union)是一家聚焦于CIO,CDO,cto,ciso,cfo,coo,chro,cpo,ceo等人群的平台组织,其中在CIO会议领域的领头羊,目前举办了大量的CIO大会、CIO论坛、CIO活动、CIO会议、CIO峰会、CIO会展。如华东CIO会议、华南cio会议、华北cio会议、中国cio会议、西部CIO会议。在这里,你可以参加大量的IT大会、IT行业会议、IT行业论坛、IT行业会展、数字化论坛、数字化转型论坛,在这里你可以认识很多的首席信息官、首席数字官、首席财务官、首席技术官、首席人力资源官、首席运营官、首席执行官、IT总监、财务总监、信息总监、运营总监、采购总监、供应链总监。

数字化转型网(资讯媒体,是企业数字化转型的必读参考,在这里你可以学习大量的知识,如财务数字化转型、供应链数字化转型、运营数字化转型、生产数字化转型、人力资源数字化转型、市场营销数字化转型。通过关注我们的公众号,你就知道如何实现企业数字化转型?数字化转型如何做?

【联盟会员】康欣新材CTO、宋都股份CTO、澄星CTO、人福医药CTO、金花股份CTO、东风科技CTO、海泰发展CTO、博信CTO、中葡CTO、同仁堂CTO、中视传媒CTO、特变电工CTO、济堂CTO、明科CTO、易见CTO、大名城CTO、湘财股份CTO、云天化CTO、开创国际CTO、广州发展CTO、林海股份CTO、同方股份CTO、明星电力CTO、青山纸业CTO、上汽集团CTO、永鼎股份CTO、重庆路桥CTO、美尔雅CTO、亚盛集团CTO、国金证券CTO、诺德股份CTO、北方稀土CTO、天成CTO、浙江东日CTO、东睦股份CTO、中国东航CTO、三峡水利CTO、西宁特钢CTO、中国卫星CTO、长江投资CTO、浙江东方CTO、郑州煤电CTO、宏图CTO、兰花科创CTO、铁龙物流CTO、杭钢股份CTO、金健米业CTO、弘业股份CTO、太极集团CTO、波导股份CTO、国网信通CTO、重庆啤酒CTO、东湖高新CTO、乐凯胶片CTO、当代文体CTO、浪莎股份CTO、中青旅CTO、西源CTO、兴发集团CTO、金发科技CTO、新亿CTO、环球CTO、长春一东CTO、廊坊发展CTO、中国船舶CTO、航天机电CTO、维科技术CTO、建发股份CTO、华创阳安CTO、华升股份CTO、永泰能源CTO、中体产业CTO、大龙地产CTO、巨化股份CTO、天坛生物CTO、香江控股CTO、中闽能源CTO、新日恒力CTO、福田汽车CTO、联美控股CTO、武汉控股CTO、太原重工CTO、上海建工CTO、上海贝岭CTO、黄河旋风CTO、卧龙地产CTO、中国巨石CTO、雅戈尔CTO、东安动力CTO、安通控股CTO、瑞茂通CTO、佳通CTO、生益科技CTO、光电股份CTO、格力地产CTO、莲花健康CTO、国中水务CTO

标签: #可视化包