楼主: sssyunsheng
2289 5

[学习分享] ggplot图说明星收入(二) [推广有奖]

  • 2关注
  • 47粉丝

已卖:107份资源

博士生

52%

还不是VIP/贵宾

-

威望
0
论坛币
3 个
通用积分
4.4708
学术水平
47 点
热心指数
49 点
信用等级
43 点
经验
5127 点
帖子
201
精华
0
在线时间
306 小时
注册时间
2012-2-21
最后登录
2025-9-22

楼主
sssyunsheng 在职认证  发表于 2015-7-4 23:23:08 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

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

经管之家联合CDA

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

感谢您参与论坛问题回答

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

+2 论坛币

绘制各国条形图

  1. temp <- aggregate(Earnings
  2. ~  country, data
  3. = star, sum)

  4. temp <- temp[order(temp$Earnings),]

  5. n <- unique(temp$country)

  6. temp$country <- factor(temp$country, levels
  7. = n)#<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:
  8. Consolas;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;
  9. mso-hansi-font-family:Consolas;mso-bidi-font-family:" times="" new="" roman";="" mso-bidi-theme-font:minor-bidi"="">这里要设置name<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:Consolas;mso-fareast-font-family:
  10. 宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Consolas;
  11. mso-bidi-font-family:" times="" new="" roman";mso-bidi-theme-font:minor-bidi"="">的level<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:Consolas;mso-fareast-font-family:
  12. 宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Consolas;
  13. mso-bidi-font-family:" times="" new="" roman";mso-bidi-theme-font:minor-bidi"="">顺序,不然ggplot<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:Consolas;mso-fareast-font-family:
  14. 宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Consolas;
  15. mso-bidi-font-family:" times="" new="" roman";mso-bidi-theme-font:minor-bidi"="">会对他重新排序

  16. theme_opts <- list(theme(panel.grid.minor = element_blank(),

  17.                          panel.grid.major.y = element_blank(),

  18.                          panel.grid.major.x = element_blank(),

  19.                          plot.background = element_rect(fill = "transparent",colour = NA),#<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:
  20. Consolas;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;
  21. mso-hansi-font-family:Consolas;mso-bidi-font-family:" times="" new="" roman";="" mso-bidi-theme-font:minor-bidi"="">设置整幅图的背景透明色

  22.                          panel.border = element_blank(),

  23.                          panel.background = element_rect(fill = "transparent",colour = NA),#<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:
  24. Consolas;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;
  25. mso-hansi-font-family:Consolas;mso-bidi-font-family:" times="" new="" roman";="" mso-bidi-theme-font:minor-bidi"="">设置绘图区的颜色为透明色

  26.                          axis.ticks.y = element_blank(),#<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:
  27. Consolas;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;
  28. mso-hansi-font-family:Consolas;mso-bidi-font-family:" times="" new="" roman";="" mso-bidi-theme-font:minor-bidi"="">改变坐标轴刻度线行为

  29.                          axis.ticks.x = element_blank(),

  30.                          axis.text.x = element_blank(),

  31.                          axis.text.y = element_text(colour="grey20",
  32. size=12),

  33.                          axis.title.y = element_blank(),#<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:
  34. Consolas;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;
  35. mso-hansi-font-family:Consolas;mso-bidi-font-family:" times="" new="" roman";="" mso-bidi-theme-font:minor-bidi"="">设置y<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:Consolas;mso-fareast-font-family:
  36. 宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Consolas;
  37. mso-bidi-font-family:" times="" new="" roman";mso-bidi-theme-font:minor-bidi"="">轴标签为空

  38.                          axis.title.x = element_blank(),

  39.                          plot.title = element_text(size=40)))

  40. p <- ggplot(temp,aes(x =
  41. country, y = Earnings)) +

  42.   geom_bar(stat = "identity",fill = rgb(red = 0, green = 137, blue = 130, max = 255)) +

  43.   theme_opts + coord_flip()
  44. +

  45.   guides(fill=FALSE)

  46. #<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:Consolas;mso-fareast-font-family:
  47. 宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Consolas;
  48. mso-bidi-font-family:" times="" new="" roman";mso-bidi-theme-font:minor-bidi"="">设置legend<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:Consolas;mso-fareast-font-family:
  49. 宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Consolas;
  50. mso-bidi-font-family:" times="" new="" roman";mso-bidi-theme-font:minor-bidi"="">的行为除了上述的guides<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:Consolas;mso-fareast-font-family:
  51. 宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Consolas;
  52. mso-bidi-font-family:" times="" new="" roman";mso-bidi-theme-font:minor-bidi"="">方式还有theme(legend.position="none")<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:
  53. Consolas;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;
  54. mso-hansi-font-family:Consolas;mso-bidi-font-family:" times="" new="" roman";="" mso-bidi-theme-font:minor-bidi"="">,这里我们设置了图表背景为透明色,为了方便绘制的图表能够和其他共组绘制的图表融合,我们z<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:Consolas;mso-fareast-font-family:
  55. 宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Consolas;
  56. mso-bidi-font-family:" times="" new="" roman";mso-bidi-theme-font:minor-bidi"="">好选择将绘图区也设置为透明,另外我们把鱼坐标轴相关的所有配件包括坐标轴标签、刻度线、坐标轴线、坐标轴名称和网格线全部设置为空,因为这样我们可以方便的将图和表融合在一起,暂时还没想到R<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:Consolas;mso-fareast-font-family:
  57. 宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Consolas;
  58. mso-bidi-font-family:" times="" new="" roman";mso-bidi-theme-font:minor-bidi"="">怎么把图和表融合在一起。

  59. #png('<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:
  60. Consolas;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;
  61. mso-hansi-font-family:Consolas;mso-bidi-font-family:" times="" new="" roman";="" mso-bidi-theme-font:minor-bidi"="">国家',width=713,height=323,units="px",bg =
  62. "transparent")

  63. #print(p)

  64. #dev.off()
复制代码

绘制气泡图

  1. temp <- aggregate(Earnings
  2. ~  country, data
  3. = star, sum)

  4. size <- as.data.frame(table(star$country))

  5. colnames(size)
  6. <- c("country",
  7. "Freq")

  8. temp <- join(temp,
  9. size)

  10. ## Joining by: country

  11. temp <- temp[order(temp$Earnings),]

  12. n <- unique(temp$country)

  13. temp$country <- factor(temp$country, levels
  14. = n)#<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:
  15. Consolas;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;
  16. mso-hansi-font-family:Consolas;mso-bidi-font-family:" times="" new="" roman";="" mso-bidi-theme-font:minor-bidi"="">这里要设置name<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:Consolas;mso-fareast-font-family:
  17. 宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Consolas;
  18. mso-bidi-font-family:" times="" new="" roman";mso-bidi-theme-font:minor-bidi"="">的level<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:Consolas;mso-fareast-font-family:
  19. 宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Consolas;
  20. mso-bidi-font-family:" times="" new="" roman";mso-bidi-theme-font:minor-bidi"="">顺序,不然ggplot<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:Consolas;mso-fareast-font-family:
  21. 宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Consolas;
  22. mso-bidi-font-family:" times="" new="" roman";mso-bidi-theme-font:minor-bidi"="">会对他重新排序

  23. temp <- cbind(temp,
  24. m = c(rep(1, length(temp[,1]))))

  25. theme_opts <- list(theme(panel.grid.minor = element_blank(),

  26.                          panel.grid.major.y = element_blank(),

  27.                          panel.grid.major.x = element_blank(),

  28.                          plot.background = element_rect(fill = "transparent",colour = NA),#<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:
  29. Consolas;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;
  30. mso-hansi-font-family:Consolas;mso-bidi-font-family:" times="" new="" roman";="" mso-bidi-theme-font:minor-bidi"="">设置整幅图的背景透明色

  31.                          panel.border = element_blank(),

  32.                          panel.background = element_rect(fill = "transparent",colour = NA),#<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:
  33. Consolas;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;
  34. mso-hansi-font-family:Consolas;mso-bidi-font-family:" times="" new="" roman";="" mso-bidi-theme-font:minor-bidi"="">设置绘图区的颜色为透明色

  35.                          axis.ticks.y = element_blank(),#<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:
  36. Consolas;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;
  37. mso-hansi-font-family:Consolas;mso-bidi-font-family:" times="" new="" roman";="" mso-bidi-theme-font:minor-bidi"="">改变坐标轴刻度线行为

  38.                          axis.ticks.x = element_blank(),

  39.                          axis.text.x = element_blank(),

  40.                          axis.text.y = element_text(colour="grey20",
  41. size=12),

  42.                          axis.title.y = element_blank(),#<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:
  43. Consolas;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;
  44. mso-hansi-font-family:Consolas;mso-bidi-font-family:" times="" new="" roman";="" mso-bidi-theme-font:minor-bidi"="">设置y<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:Consolas;mso-fareast-font-family:
  45. 宋体;mso-fareast-theme-font:minor-fareast;mso-hansi-font-family:Consolas;
  46. mso-bidi-font-family:" times="" new="" roman";mso-bidi-theme-font:minor-bidi"="">轴标签为空

  47.                          axis.title.x = element_blank(),

  48.                          plot.title = element_text(size=40)))

  49. p <- ggplot(temp,aes(x =
  50. country, y = m, size = Freq)) +

  51.   geom_point(colour = rgb(red = 252, green = 102, blue = 129, max = 255), fill="white") + coord_flip()
  52. +

  53.   scale_size_area(max_size = 25) +

  54.   #geom_text(data=temp,aes(x = country, y = m,
  55. label = Freq, size = 4)) +

  56.   theme_opts +

  57.   guides(size = FALSE)

  58. #png('<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:
  59. Consolas;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;
  60. mso-hansi-font-family:Consolas;mso-bidi-font-family:" times="" new="" roman";="" mso-bidi-theme-font:minor-bidi"="">国家人数',width=232,height=1000,units="px",bg =
  61. "transparent")

  62. #print(p)

  63. #dev.off()
复制代码

国家vs人数.png


各行业收入比例

  1. temp <- aggregate(Earnings
  2. ~  Category, data
  3. = star, summary)

  4. write.csv(temp,
  5. "H:/<span lang="EN-US" style="font-family:宋体;mso-ascii-font-family:
  6. Consolas;mso-fareast-font-family:宋体;mso-fareast-theme-font:minor-fareast;
  7. mso-hansi-font-family:Consolas;mso-bidi-font-family:" times="" new="" roman";="" mso-bidi-theme-font:minor-bidi"="">自媒体/2015-07-04/Category.csv")
复制代码

职业.png


各行业最高收入,平均收入即summary


相关数据和代码:http://pan.baidu.com/s/1g6kQa   密码:微信索取
关于我们,关注理性与文艺,用数据创作内容性的精致阅读,这里是数据分析挖掘人员与文艺青年的集结地,不做培训,不做鼓吹,只踏踏实实的做一个又一个数据驱动的文章,并设计机器人减轻数据分析的负担,无论你感兴趣还是想参与都可以关注,请加微信公众号大音如霜

0.jpg


二维码

扫码加我 拉你入群

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

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

关键词:gplot plot GPL Temp TEM ggplot2 气泡图

沙发
ofzhengyi 发表于 2015-7-5 10:17:52
LZ牛人,但代码有问题,需要重新排版~

藤椅
sssyunsheng 在职认证  发表于 2015-7-25 22:13:41
ofzhengyi 发表于 2015-7-5 10:17
LZ牛人,但代码有问题,需要重新排版~
网盘有代码,不知道我的代码传上来就变了

板凳
kingswordsc 发表于 2015-7-26 07:57:36
nb,支持一下!

报纸
qiangli 发表于 2015-7-26 08:01:19
最好程序最前面能够写上 调用一个R自带的数据集的命令,
这样大家更加容易学习

地板
sssyunsheng 在职认证  发表于 2015-7-27 23:07:12
qiangli 发表于 2015-7-26 08:01
最好程序最前面能够写上 调用一个R自带的数据集的命令,
这样大家更加容易学习
我们的代码是跟着微信文章走的哦

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-5 17:11