gpt4 book ai didi

How to complete barplot bars to 100% with a distinct style?(如何将条形图条形图做到100%,风格鲜明?)

转载 作者:bug小助手 更新时间:2023-10-25 21:30:15 25 4
gpt4 key购买 nike



Plotting a bar plot where Y axis reflects percents, how can I "complete" each bar to 100%?

在绘制Y轴反映百分比的条形图时,我如何才能将每个条形图“完成”到100%?


Example


Consider the following plot:

请考虑以下曲线图:


library(ggplot2)
library(tibble)

my_df <- tribble(~step, ~prop,
"a", 1,
"b", 0.8,
"c", 0.55,
"d", 0.2
)

my_df |>
ggplot(aes(x = step,
y = prop)) +
geom_col()


Created on 2023-09-10 with reprex v2.0.2

创建于2023-09-10,Reprex v2.0.2


How can I create a plot that completes each bar to 100% out of it with dashed lines? Something like:
enter image description here

我如何创建一个用虚线100%地完成每个条形图的绘图?类似于:


更多回答
优秀答案推荐


One option would be use a second geom_col to first draw 100% bars, then place your bars on top of it where we have to explicitly set the outline color. Note: One downside, which I did not realize at first, is that the outline of the background bars shouldn't but is visible. A fix would be to set the linewidth and or the bar width as suggested by @jkd and @r2evans in their comments. A second and cleaner option would be to use a geom_path (see below).

一种选择是使用第二个geom_ol首先绘制100%的条形图,然后将条形图放置在必须显式设置轮廓颜色的位置。注意:一开始我没有意识到的一个缺点是,背景栏的轮廓不应该但却是可见的。一个解决办法是按照@jkd和@r2evans在他们的评论中建议的那样设置行宽和/或条宽。第二个更简洁的选项是使用geom_path(如下所示)。


library(ggplot2)
library(tibble)

my_df <- tribble(
~step, ~prop,
"a", 1,
"b", 0.8,
"c", 0.55,
"d", 0.2
)

my_df |>
ggplot(aes(
x = step,
y = prop
)) +
geom_col(aes(y = 1), color = "red", linetype = "dashed", fill = NA) +
geom_col(color = "grey35")


IMHO, the cleanest option to achieve the same result would be to use a geom_path which however requires some effort to create an appropriate dataset to draw the paths:

IMHO,要获得相同的结果,最干净的选择是使用geom_path,但这需要一些工作来创建适当的数据集来绘制路径:


library(ggplot2)
library(tibble)
library(dplyr, warn = FALSE)

make_path <- function(y, width = .9) {
if (y < 1) {
data.frame(
x = width / 2 * c(-1, -1, 1, 1),
y = c(y, 1, 1, y)
)
} else {
data.frame(x = numeric(0), y = numeric(0))
}
}

df_path <- my_df |>
mutate(step = factor(step)) |>
reframe(make_path(prop), .by = step) |>
mutate(x = as.numeric(step) + x)

my_df |>
ggplot(aes(
x = step,
y = prop
)) +
geom_col(color = "grey35") +
geom_path(
data = df_path,
aes(x = x, y = y, group = step),
color = "red", linetype = "dashed"
)



Yet another way with stacked bars and controlling alpha and manual scaling of properties:

另一种方法是堆叠条形图并控制Alpha和手动缩放属性:


library(ggplot2)
library(tibble)

my_df <- tribble(~step, ~prop,
"a", 1,
"b", 0.8,
"c", 0.55,
"d", 0.2
)
my_df$type <- 'filled'

my_df2 <- my_df
my_df2$prop <- 1 - my_df2$prop
my_df2$type <- 'empty'

my_df <- rbind(my_df, my_df2)

my_df |> ggplot(aes(x = step, y = prop)) +
geom_col(aes(alpha=type, col=type, linetype=type)) +
scale_linetype_manual(values=c('dashed','solid')) +
scale_color_manual(values=c('red', 'gray')) +
scale_alpha_manual(values=c(0, 1)) + theme(legend.position = "none")

enter image description here



In base R we can use rect.

在基数R中,我们可以使用RECT。


plot(d$prop, xlim=c(.5, 4.5), ylim=0:1, type='n', xaxt='n', ylab='Proportion', xlab='Step')
axis(1, axTicks(1), labels=d$step)
sq <- seq_len(nrow(d))
rect(sq - .3, 0, sq + .3, rep(1, nrow(d)), lty=2, border=2)
rect(sq - .3, 0, sq + .3, d$prop, col=8, border=8, lwd=2)

enter image description here


Data:

数据:


d <- structure(list(step = c("a", "b", "c", "d"), prop = c(1, 0.8, 
0.55, 0.2)), class = "data.frame", row.names = c(NA, -4L))

更多回答

Thanks! This solution is nice and quick. However, it wraps the entire rectangle with the dashed line, so with different colors (other than grey) it's noticeable and becomes confusing.

谢谢!这个解决方案又好又快。然而,它用虚线包围了整个矩形,所以用不同的颜色(不是灰色)会很明显,也会让人感到困惑。

In addition to explicitly setting the color you could also slightly increase the grey bar's linewidth.

除了显式地设置颜色,您还可以稍微增加灰色条的线宽。

Option 1, as jkd suggested: set the line width, geom_col(color = "grey35", linewidth=2). Option 2 (new), reduce the width of the red bars a little. They start at 90% of the data resolution (which is 1 here, so 0.9 is default width), we can do: geom_col(aes(y = 1), color = "red", linetype = "dashed", fill = NA, width = 0.89).

选项1,如jkd所建议的:设置行宽geom_ol(color=“grey35”,linewidth=2)。选项2(新),将红色线条的宽度略微缩小。它们以数据分辨率的90%开始(这里是1,所以0.9是默认宽度),我们可以这样做:geom_ol(aes(y=1),color=“red”,linetype=“dash”,ill=na,idth=0.89)。

Thanks @jkd and @r2evans for your comments which both offer valid fixes. Just added a second option using a geom_path which extends the bars and avoids the issue with the outline.

感谢@jkd和@r2evans的评论,它们都提供了有效的修复。我刚刚使用geom_path添加了第二个选项,该选项扩展了条形图并避免了轮廓问题。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com