楼主: oliyiyi
1250 0

How to add a background image to ggplot2 graphs [推广有奖]

版主

已卖:2993份资源

泰斗

1%

还不是VIP/贵宾

-

TA的文库  其他...

计量文库

威望
7
论坛币
117070 个
通用积分
31670.9540
学术水平
1454 点
热心指数
1573 点
信用等级
1364 点
经验
384134 点
帖子
9629
精华
66
在线时间
5508 小时
注册时间
2007-5-21
最后登录
2025-7-8

初级学术勋章 初级热心勋章 初级信用勋章 中级信用勋章 中级学术勋章 中级热心勋章 高级热心勋章 高级学术勋章 高级信用勋章 特级热心勋章 特级学术勋章 特级信用勋章

楼主
oliyiyi 发表于 2016-11-10 10:21:40 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

When producing so called infographics, it is rather common to use images rather than a mere grid as background. In this blog post, I will show how to use a background image with ggplot2.

Packages required

The following code will install load and / or install the R packages required for this blog post.

  1. if (!require("pacman")) install.packages("pacman")
  2. pacman::p_load(jpeg, png, ggplot2, grid, neuropsychology)
复制代码

Choosing the data

The data set I will be using in this blog post is named diamonds and part of the ggplot2 package. It contains information about – surprise, surprise – diamonds, e.g. price and cut (Fair, Good, Very Good, Premium, Ideal). Using the tapply-function, we create a table returning the maximum prices per cut. Since we need the data to be organized in a data frame, we must transform the table using the data.frame-function.

  1. mydata <- data.frame(price = tapply(diamonds$price, diamonds$cut, max))
  2. mydata$cut <- rownames(mydata)
复制代码

cutprice
Fair18574
Good18788
Very Good18818
Premium18823
Ideal18806Importing the background image

The file format of the background image we will be using in this blog post is JPG. Since the image imitates a blackboard, we name it “blackboard.jpg”. The image file must be imported using the readJPEG-function of the jpeg package. The imported image will be saved into an object named image.

  1. imgage <- jpeg::readJPEG("blackboard.jpg")
复制代码

To import other image file formats, different packages and functions must be used. The next code snippet shows how to import PNG images.

  1. image <- png::readPNG("blackboard.png")
复制代码

Drawing the plot

In the next step, we actually draw a bar chart with a backgriund image. To make blackboard.jpg the background image, we need to combine the annotation_custom-function of the ggplot2 package and the rasterGrob-function of the grid package.

  1. ggplot(mydata, aes(cut, price, fill = -price)) +
  2.   ggtitle("Bar chart with background image") +
  3.   scale_fill_continuous(guide = FALSE) +
  4.   annotation_custom(rasterGrob(imgage,  
  5.                                width = unit(1,"npc"),  
  6.                                height = unit(1,"npc")),  
  7.                                -Inf, Inf, -Inf, Inf) +
  8.   geom_bar(stat="identity", position = "dodge", width = .75, colour = 'white') +
  9.   scale_y_continuous('Price in

  10. [backcolor=rgb(34, 34, 34)][color=rgb(41, 107, 204)][url=http://datascienceplus.com/wp-content/uploads/2016/10/plot-background2.png][color=rgb(255, 255, 255) !important][/color][/url][/color][/backcolor]
  11. [color=rgb(41, 107, 204)][url=http://datascienceplus.com/wp-content/uploads/2016/10/plot-background2.png][img=0,327]http://datascienceplus.com/wp-content/uploads/2016/10/plot-background2-490x327.png[/img][/url][/color]

  12. [b]Adding opacity[/b][align=left][color=#383838][font=Arial, Helvetica, sans-serif]Using the specification alpha = 0.5, we add 50% opacity to the bars. alpha ranges between 0 and 1, with higher values indicating greater opacity.[/font][/color][/align] [code]ggplot(mydata, aes(cut, price, fill = -price)) +
  13.   theme_neuropsychology() +
  14.   ggtitle("Bar chart with background image") +
  15.   scale_fill_continuous(guide = FALSE) +
  16.   annotation_custom(rasterGrob(imgage,  
  17.                                width = unit(1,"npc"),  
  18.                                height = unit(1,"npc")),  
  19.                                -Inf, Inf, -Inf, Inf) +
  20.   geom_bar(stat="identity", position = "dodge", width = .75, colour = 'white', alpha = 0.5) +
  21.   scale_y_continuous('Price in

  22. [backcolor=rgb(34, 34, 34)][color=rgb(41, 107, 204)][url=http://datascienceplus.com/wp-content/uploads/2016/10/plot-background.png][color=rgb(255, 255, 255) !important][/color][/url][/color][/backcolor]
  23. [color=rgb(41, 107, 204)][url=http://datascienceplus.com/wp-content/uploads/2016/10/plot-background.png][img=0,327]http://datascienceplus.com/wp-content/uploads/2016/10/plot-background-490x327.png[/img][/url][/color]

  24. [align=left][color=#383838][font=Arial, Helvetica, sans-serif]The recently published R package neuropsychology contains a theme named theme_neuropsychology(). This theme may be used to get bigger axis titles as well as bigger axis and legend text.[/font][/color][/align][align=left][color=#383838][font=Arial, Helvetica, sans-serif]I hope you find this post useful and If you have any question please post a comment below. You are welcome to visit my personal blog [color=rgb(41, 107, 204)][url=https://scriptsandstatistics.wordpress.com/]Scripts and Statistics[/url][/color] for more R tutorials.[/font][/color][/align]
  25. , limits = c(0, max(mydata$price) + max(mydata$price) / 4)) +
  26.   scale_x_discrete('Cut') +
  27.   geom_text(aes(label = round(price), ymax = 0), size = 7, fontface = 2,  
  28.             colour = 'white', hjust = 0.5, vjust = -1)  
复制代码


[color=rgb(255, 255, 255) !important]


Adding opacity

Using the specification alpha = 0.5, we add 50% opacity to the bars. alpha ranges between 0 and 1, with higher values indicating greater opacity.

[        DISCUZ_CODE_7        ]

[color=rgb(255, 255, 255) !important]


The recently published R package neuropsychology contains a theme named theme_neuropsychology(). This theme may be used to get bigger axis titles as well as bigger axis and legend text.

I hope you find this post useful and If you have any question please post a comment below. You are welcome to visit my personal blog Scripts and Statistics for more R tutorials.


, limits = c(0, max(mydata$price) + max(mydata$price) / 4)) +
  scale_x_discrete('Cut') +
  geom_text(aes(label = round(price), ymax = 0), size = 7, fontface = 2,  
            colour = 'white', hjust = 0.5, vjust = -1)  [/code]

[color=rgb(255, 255, 255) !important]


The recently published R package neuropsychology contains a theme named theme_neuropsychology(). This theme may be used to get bigger axis titles as well as bigger axis and legend text.

I hope you find this post useful and If you have any question please post a comment below. You are welcome to visit my personal blog Scripts and Statistics for more R tutorials.


, limits = c(0, max(mydata$price) + max(mydata$price) / 4)) +
  scale_x_discrete('Cut') +
  geom_text(aes(label = round(price), ymax = 0), size = 7, fontface = 2,  
            colour = 'white', hjust = 0.5, vjust = -1)  [/code]

[color=rgb(255, 255, 255) !important]


Adding opacity

Using the specification alpha = 0.5, we add 50% opacity to the bars. alpha ranges between 0 and 1, with higher values indicating greater opacity.

[        DISCUZ_CODE_7        ]

[color=rgb(255, 255, 255) !important]


The recently published R package neuropsychology contains a theme named theme_neuropsychology(). This theme may be used to get bigger axis titles as well as bigger axis and legend text.

I hope you find this post useful and If you have any question please post a comment below. You are welcome to visit my personal blog Scripts and Statistics for more R tutorials.


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Background ggplot2 Graphs Ground ROUND background

缺少币币的网友请访问有奖回帖集合
https://bbs.pinggu.org/thread-3990750-1-1.html

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-9 17:34