假设我有以下dataframe
在R
:
df1 <- read.csv("jan.csv", stringsAsFactors = FALSE, header = TRUE)
str(df1)
'data.frame': 4 obs. of 5 variables:
$ JANUARY: chr "D-150" "D-90" "D-60" "D-30"
$ X2016 : num 0.24 0.5 0.63 0.76
$ X2017 : num 0.32 0.45 0.6 0.79
$ X2018 : num 0.2 0.4 0.61 0.82
$ X2019 : num 0.21 0.35 0.63 0.85
如何使用ggplot2
输出的图形像一个下方(Excel
制造):
我很舒服生产中column chart
简单ggplot2
但我如上图所示,并把相关标签奋力组吧。另外,我需要重塑数据来实现这一目标?
是的你可以。我觉得你的一年的标签是不正确的。检查我的情节:
下面是产生剧情的代码:
library(tidyverse)
df1 %>%
gather(year, value, X2016:X2019) %>%
mutate(JANUARY = JANUARY %>% fct_rev() %>% fct_relevel('D-150')) %>%
group_by(JANUARY) %>%
mutate(y_pos = min(value) / 2) %>%
ggplot(aes(
x = JANUARY,
y = value,
fill = JANUARY,
group = year
)) +
geom_col(
position = position_dodge(.65),
width = .5
) +
geom_text(aes(
y = value + max(value) * .03,
label = round(value * 100) %>% str_c('%')
),
position = position_dodge(.65)
) +
geom_text(aes(
y = y_pos,
label = str_remove(year, 'X')
),
color = 'white',
angle = 90,
fontface = 'bold',
position = position_dodge(.65)
) +
scale_y_continuous(
breaks = seq(0, .9, .1),
labels = function(x) round(x * 100) %>% str_c('%')
) +
scale_fill_manual(values = c(
rgb(47, 85, 151, maxColorValue = 255),
rgb(84, 130, 53, maxColorValue = 255),
rgb(244, 177, 131, maxColorValue = 255),
rgb(112, 48, 160, maxColorValue = 255)
)) +
theme(
plot.title = element_text(hjust = .5),
panel.background = element_blank(),
panel.grid.major.y = element_line(color = rgb(.9, .9, .9)),
axis.ticks = element_blank(),
legend.position = 'none'
) +
xlab('') +
ylab('') +
ggtitle('Month of JANUARY')
随着越来越多一点点数据处理我想你可以达到你想要的东西。我们通过熔融数据为长格式是什么ggplot
需要这种类型的情节开始。然后我们创建一个单独的标签数据集包含y值(似乎是每一个“d”组内分钟):
df_m <- melt(df, id.vars = "JANUARY")
df_m$above_text <- scales::percent(df_m$value)
labels <- df_m
labels$value <- ave(labels$value, labels$JANUARY, FUN = function(x) min(x/2))
labels$variable <- sub("X", "", labels$variable)
pos_d <- position_dodge(width = 0.7)
ggplot(df_m, aes(x = JANUARY, y = value, group = variable, fill = JANUARY)) +
geom_col(width = 0.6, position = pos_d) +
geom_text(aes(label = above_text), position = pos_d, size = 2, hjust = 0.5, vjust = -1) +
geom_text(data = labels, aes(x = JANUARY, y = value, group = variable, label = variable), angle = 90, position = pos_d, hjust = 0.5)
注意,你可以玩的%标签尺寸。有什么好看取决于你的图像文件的实际尺寸。什么找过我好约为2.75,但看起来拥挤复制这里的图像。
数据:
df <- data.frame(JANUARY = c("D-150", "D-90", "D-60", "D-30"),
X2016 = c(0.24, 0.5, 0.63, 0.76),
X2017 = c(0.32, 0.45, 0.6, 0.79),
X2018 = c(0.2, 0.4, 0.61, 0.82),
X2019 = c(0.21, 0.35, 0.63, 0.85), stringsAsFactors = FALSE)
我的方法
样本数据
library( data.table )
dt <- fread('year "D-150" "D-90" "D-60" "D-30"
2016 0.24 0.5 0.63 0.76
2017 0.32 0.45 0.6 0.79
2018 0.2 0.4 0.61 0.82
2019 0.21 0.35 0.63 0.85', header = TRUE)
码
#first, melt
dt.melt <- melt( dt, id.vars = "year", variable.name = "Dvalue", value.name = "value" )
#create values (=positions in the chart) for the year-text within the bars.
dt.melt[, yearTextPos := min( value / 2 ), by = "Dvalue"]
#then build chart
library( ggplot2 )
library( scales)
ggplot( dt.melt, aes( x = Dvalue, y = value, group = year, fill = Dvalue ) ) +
#build the bars, dodged position
geom_col( width = 0.6, position = position_dodge(width = 0.75) ) +
#set up the y-scale
scale_y_continuous( limits = c(0,1), breaks = seq(0,1,0.1),
labels = scales::percent, expand = c(0,0) ) +
#insert year-text in bars, at the previuously calculated positions
geom_text( aes( x = Dvalue, y = yearTextPos, group = year, label = year ),
color = "white", position = position_dodge( width = 0.75 ),
hjust = 0.5, angle = 90, size = 5 ) +
#wite value on top as percentage
geom_text( aes( x = Dvalue, y = value + 0.01, group = year,
label = paste0( round( value * 100), "%" ) ),
color = "black", position = position_dodge( width = 0.75 ),
hjust = 0.5, angle = 0, size = 3 )
是的,是可行的。但是,首先我们需要有真正的表格格式的数据(如果你出口到SQL)。
所以,这是你的数据:
January = c("D-150","D-90","D-60")
x2016 = c(0.24 , 0.5, 0.63)
x2017 = c(0.32 , 0.45, 0.6)
x2018 = c(0.2 , 0.4 , 0.61)
df1 <- data.frame(January,x2016,x2017,x2018)
为了得到它的方式来绘制,我们要对您的年列合并成2列,这样的:
library(tidyr)
nuevoDf1<-gather(data = df1, losAnhos,valores,-January)
结果会是这样的:
January losAnhos valores
1 D-150 x2016 0.24
2 D-90 x2016 0.50
3 D-60 x2016 0.63
4 D-150 x2017 0.32
5 D-90 x2017 0.45
最后,使用GGPLOT2,你可以开始您的图表:
ggplot(nuevoDf1,aes(losAnhos,valores)) +
facet_wrap(~January)+
geom_bar(stat="sum",na.rm=TRUE)
其结果将是这样的一个的图片。我不是颜色的忠实粉丝,但GGPLOT2允许的情节已建成的自定义之后。希望你设定在正确的道路上刚刚找出图形的短暂和瞬息之美。
首先,我从广角转换数据与gather
长格式,然后把原来的列名(X2016
,X2017
,...)与parse_number
数值变量。我用fct_inorder
命令在它们出现的顺序JANUARY
的水平。
library(tidyverse)
df1_long <- df1 %>%
gather(year, percentage, -JANUARY) %>%
mutate(year = parse_number(year),
JANUARY = fct_inorder(JANUARY))
df1_long
# JANUARY year percentage
# 1 D-150 2016 0.24
# 2 D-90 2016 0.50
# 3 D-60 2016 0.63
# 4 D-30 2016 0.76
# 5 D-150 2017 0.32
# 6 D-90 2017 0.45
# 7 D-60 2017 0.60
# 8 D-30 2017 0.79
# 9 D-150 2018 0.20
# 10 D-90 2018 0.40
# 11 D-60 2018 0.61
# 12 D-30 2018 0.82
# 13 D-150 2019 0.21
# 14 D-90 2019 0.35
# 15 D-60 2019 0.63
# 16 D-30 2019 0.85
然后,这些数据可用于绘制。
ggplot(df1_long, aes(year, percentage, fill = JANUARY)) +
geom_col() +
scale_y_continuous(labels = scales::percent, expand = c(0, 0), limits = c(0, 1)) +
facet_wrap(~ JANUARY, nrow = 1, strip.position = "bottom") +
geom_text(aes(label = year), y = 0.1, angle = 90, color = "white") +
geom_text(aes(label = str_c(percentage*100, "%")), vjust = -0.5) +
ggtitle("Month of JANUARY") +
scale_fill_manual(values = c("darkblue", "darkgreen", "burlywood2", "darkorchid4")) +
theme_minimal() +
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
axis.title = element_blank(),
panel.spacing = unit(0, "cm"),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
legend.position = "none")
数据
df1 <- data.frame(JANUARY = c("D-150", "D-90", "D-60", "D-30"),
X2016 = c(0.24, 0.5, 0.63, 0.76),
X2017 = c(0.32, 0.45, 0.6, 0.79),
X2018 = c(0.2, 0.4, 0.61, 0.82),
X2019 = c(0.21, 0.35, 0.63, 0.85))