R可视化:另类的柱状图

介绍

方格状态的柱状图

加载R包

{r} 复制代码
knitr::opts_chunk$set(echo = TRUE, message = FALSE, warning = FALSE)
library(patternplot)
library(png)
library(ggplot2)
library(gridExtra)

rm(list = ls())
options(stringsAsFactors = F)

导入数据

{r, 复制代码
data <- read.csv(system.file("extdata", "monthlyexp.csv", package="patternplot"))

准备数据

{r, 复制代码
data <- data[which(data$Location == 'City 1'), ]
x <- factor(data$Type, c('Housing', 'Food', 'Childcare'))
y <- data$Amount
pattern.type <- c('hdashes', 'blank', 'crosshatch')
pattern.color <- c('black', 'black', 'black')
background.color <- c('white','white', 'white')
density <- c(20, 20, 10)

画图

{r, 复制代码
barp1 <- patternbar(
  data, x, y,
  group = NULL,
  ylab = 'Monthly Expenses, Dollars', 
  pattern.type = pattern.type, hjust=0.5,
  pattern.color = pattern.color, 
  background.color = background.color,pattern.line.size = c(5.5, 1, 4),
  frame.color = c('black', 'black', 'black'), 
  density = density) + 
  scale_y_continuous(limits = c(0, 2800)) +
  ggtitle('(A) Black and White with Patterns')

pattern.color <- c('black','white', 'grey20')
background.color <- c('lightgreen','lightgreen', 'lightgreen')
barp2 <- patternbar(
  data, x, y, 
  group = NULL,
  ylab = 'Monthly Expenses, Dollars', 
  pattern.type = pattern.type, hjust=0.5,
  pattern.color = pattern.color, 
  background.color = background.color,
  pattern.line.size = c(5.5, 1, 4),
  frame.color = c('black', 'black', 'black'), 
  density = density) +
  scale_y_continuous(limits = c(0, 2800)) +
  ggtitle('(B) Colors with Patterns')


grid.arrange(barp1,barp2,  nrow = 1)
相关推荐
特立独行的猫a1 分钟前
C++开发中的构建工具:现代CMake实战速成
开发语言·c++·cmake·入门教程
朝花不迟暮7 分钟前
Go基础-闭包
android·开发语言·golang
Wpa.wk13 分钟前
自动化测试(java) - PO模式了解
java·开发语言·python·测试工具·自动化·po模式
徐先生 @_@|||14 分钟前
Java/Maven 对比 Python/PyPI
开发语言·python
编程猪猪侠20 分钟前
手写js轮播图效果参考
开发语言·javascript·ecmascript
思成不止于此28 分钟前
C++ STL中map与set的底层实现原理深度解析
开发语言·c++·set·map·红黑树·底层实现
惺忪979830 分钟前
C++ 构造函数完全指南
开发语言·c++
小此方32 分钟前
Re:从零开始学C++(五)类和对象·第二篇:构造函数与析构函数
开发语言·c++
秦苒&32 分钟前
【C语言】详解数据类型和变量(二):三种操作符(算数、赋值、单目)及printf
c语言·开发语言·c++·c#
无限进步_33 分钟前
【C语言&数据结构】有效的括号:栈数据结构的经典应用
c语言·开发语言·数据结构·c++·git·github·visual studio