我需要绘制一个显示计数的条形图和一个在一个图表中显示速率的折线图,我可以分别做两个,但是当我把它们放在一起时,我的第一层的比例(即geom_bar)与第二层重叠层(即geom_line).
我可以将轴geom_line向右移动吗?
我有double-y-axis图表制作Excel.在Excel中,它只需要基本技能.我想做的是使用ggplot2库复制此图表R.
我已经这样做了,但我需要绘制Response 2nd-y-axis.
我附上了我用过的可重现的代码:
#Data generation
Year <- c(2014, 2015, 2016)
Response <- c(1000, 1100, 1200)
Rate <- c(0.75, 0.42, 0.80)
df <- data.frame(Year, Response, Rate)
#Chart
library(ggplot2)
ggplot(df) +
geom_bar(aes(x=Year, y=Response),stat="identity", fill="tan1", colour="sienna3")+
geom_line(aes(x=Year, y=Rate),stat="identity")+
geom_text(aes(label=Rate, x=Year, y=Rate), colour="black")+
geom_text(aes(label=Response, x=Year, y=0.9*Response), colour="black")
Run Code Online (Sandbox Code Playgroud) 下面的代码使用R中的基本绘图函数创建一个帕累托图.如何使用ggplot创建相同的图表?
*我知道有些人会讨厌两个y轴的情节.请不要在这篇文章中包含这个辩论.谢谢
## Creating the d tribble
library(tidyverse)
d <- tribble(
~ category, ~defect,
"price", 80,
"schedule", 27,
"supplier", 66,
"contact", 94,
"item", 33
)
## Creating new columns
d <- arrange(d, desc(defect)) %>%
mutate(
cumsum = cumsum(defect),
freq = round(defect / sum(defect), 3),
cum_freq = cumsum(freq)
)
## Saving Parameters
def_par <- par()
## New margins
par(mar=c(5,5,4,5))
## bar plot, pc will hold x values for bars
pc = barplot(d$defect,
width = 1, space = 0.2, border = …Run Code Online (Sandbox Code Playgroud) 我正在尝试创建一个ggplot2图形,该图形显示相互重叠的条形图和折线图。在excel中,这可以通过添加第二个轴来完成。
x轴表示产品类型,条形图的y值应表示收入,而我想将利润率表示为百分比的折线图。折线图和条形图的值应彼此独立,即不存在这种关系。
require(ggplot2)
df <- data.frame(x = c(1:5), y = abs(rnorm(5)*100))
df$y2 <- abs(rnorm(5))
ggplot(df, mapping= aes(x=as.factor(`x`), y = `y`)) +
geom_col(aes(x=as.factor(`x`), y = `y`),fill = 'blue')+
geom_line(mapping= aes(x=as.factor(`x`), y = `y`),group=1) +
geom_label(aes(label= round(y2,2))) +
scale_y_continuous() +
theme_bw() +
theme(axis.text.x = element_text(angle = 20,hjust=1))
Run Code Online (Sandbox Code Playgroud)
上面的图像几乎产生了我想要的。但是,缩放比例不正确-我需要按幅度对1.38和0.23值进行排序,即0.23点应显示在1.38以下。我也不确定如何在右侧添加另一个轴。