修改 alpha 后,图像透明度会受到影响(透明)!图表2
Image transparency is affected (transparent) once alpha is modified! ggplot2
我正在尝试将以下图像添加到此条形图中,但是,正如您所看到的,当我将其设置为 pdf 时,图像变得透明了! alpha 为 0.45。如果我将 alpha 设置为 1,那么它应该会显示!
在这个例子中,我只使用 alpha 来使第二个闪避条形图透明,但包含的 png 应该是 100% 颜色不褪色!你知道有什么方法可以做到这一点吗,我仍然希望那些条带随着 alpha 逐渐消失,但图像完好无损。
请注意,如果您只是 运行 在您的 Rmarkdown 中使用此图片,预览中的图像看起来非常好,当我们编织并生成 pdf 时,问题就来了!
请帮忙
这是 knitr 到 pdf 时图表上马图像的褪色结果:
实际图标是:
我使用的代码在这里:
library(ggplot2)
library(reshape2)
library(grid)
#Create DataFrame
x <- c(5,17,31,9,17,10,30,28,16,29,14,34)
y <- c(39,6,3,4,1,6,7,2,9,10,3,4)
day <- c("d1","cd2","d3","d4","d5","d6","d7","d8","d9","d10","d11","d12")
df1 <- data.frame(x, y, day)
#Sort the table according to x values
df1$day <- factor(df1$day, levels = df1[order(-df1$x), 3])
#for each day (e.g. 1) you will have all combinations of x and y (e.g. day = 1, x = 5, y = 5), (e.g. day = 12, x=34, y=4). variable becomes x and y, and value becomes the value of either x or y.
df2 <- melt(df1, id.var="day", measure.var=c("x","y"))
#head(df2)
#Image
img <- readPNG("imgs/unnamed.png")
g <- rasterGrob(img, interpolate=TRUE)
barcolors <- c("#428953", "#CE2929", "#A3DD57", "#77E599", "#5675D6", "#65ECEF", "#FF8B07", "#D0B100", "#636363", "#D0B100", "#CE2929", "#428953")
ggplot(df2, aes(x=day, y=value)) +
geom_bar(aes(fill=day, alpha=variable), stat="identity", width = .75, position = position_dodge(width=.8)) +
scale_alpha_manual(values=c(1, .45)) +
scale_fill_manual(values= barcolors) +
theme_bw() +
theme( panel.grid.major.x = element_blank(),
legend.position="none",
legend.text=element_text(size=14),
legend.title = element_blank(),
axis.title.x = element_blank()) +
annotation_custom(g, xmin=.7, xmax=1.3, ymin=35, ymax=40) + ylim(0,40)
这可能不是原因的解释,而是可行的解决方案。
library(tidyverse)
library(reshape2)
library(grid)
library(png)
df = tibble(
x = c(5,17,31,9,17,10,30,28,16,29,14,34),
y = c(39,6,3,4,1,6,7,2,9,10,3,4),
day = c("d1","cd2","d3","d4","d5","d6","d7","d8","d9","d10","d11","d12")
) %>% mutate(
day = day %>% fct_reorder(x)
) %>% pivot_longer(cols=1:2, names_to = "variable", values_to = "value")
img <- readPNG("imgs/unnamed.png")
g <- rasterGrob(img, interpolate=TRUE)
barcolors <- c("#428953", "#CE2929", "#A3DD57", "#77E599", "#5675D6", "#65ECEF", "#FF8B07", "#D0B100", "#636363", "#D0B100", "#CE2929", "#428953")
df %>% ggplot(aes(x=day, y=value, alpha=variable)) +
geom_bar(aes(fill=day), stat="identity", width = .75, position = position_dodge(width=.8))+
annotation_custom(g, xmin=.7, xmax=1.3, ymin=35, ymax=40)+
annotation_custom(g, xmin=.7, xmax=1.3, ymin=35, ymax=40)+
scale_alpha_manual(values=c(1, .45)) +
scale_fill_manual(values= barcolors) +
theme_bw() +
theme( panel.grid.major.x = element_blank(),
legend.position="none",
legend.text=element_text(size=14),
legend.title = element_blank(),
axis.title.x = element_blank()) +
ylim(0,40)
以下是 pdf 文件的摘录。
这是来自 HTML 文件的片段。
我正在尝试将以下图像添加到此条形图中,但是,正如您所看到的,当我将其设置为 pdf 时,图像变得透明了! alpha 为 0.45。如果我将 alpha 设置为 1,那么它应该会显示!
在这个例子中,我只使用 alpha 来使第二个闪避条形图透明,但包含的 png 应该是 100% 颜色不褪色!你知道有什么方法可以做到这一点吗,我仍然希望那些条带随着 alpha 逐渐消失,但图像完好无损。
请注意,如果您只是 运行 在您的 Rmarkdown 中使用此图片,预览中的图像看起来非常好,当我们编织并生成 pdf 时,问题就来了!
请帮忙
这是 knitr 到 pdf 时图表上马图像的褪色结果:
实际图标是:
我使用的代码在这里:
library(ggplot2)
library(reshape2)
library(grid)
#Create DataFrame
x <- c(5,17,31,9,17,10,30,28,16,29,14,34)
y <- c(39,6,3,4,1,6,7,2,9,10,3,4)
day <- c("d1","cd2","d3","d4","d5","d6","d7","d8","d9","d10","d11","d12")
df1 <- data.frame(x, y, day)
#Sort the table according to x values
df1$day <- factor(df1$day, levels = df1[order(-df1$x), 3])
#for each day (e.g. 1) you will have all combinations of x and y (e.g. day = 1, x = 5, y = 5), (e.g. day = 12, x=34, y=4). variable becomes x and y, and value becomes the value of either x or y.
df2 <- melt(df1, id.var="day", measure.var=c("x","y"))
#head(df2)
#Image
img <- readPNG("imgs/unnamed.png")
g <- rasterGrob(img, interpolate=TRUE)
barcolors <- c("#428953", "#CE2929", "#A3DD57", "#77E599", "#5675D6", "#65ECEF", "#FF8B07", "#D0B100", "#636363", "#D0B100", "#CE2929", "#428953")
ggplot(df2, aes(x=day, y=value)) +
geom_bar(aes(fill=day, alpha=variable), stat="identity", width = .75, position = position_dodge(width=.8)) +
scale_alpha_manual(values=c(1, .45)) +
scale_fill_manual(values= barcolors) +
theme_bw() +
theme( panel.grid.major.x = element_blank(),
legend.position="none",
legend.text=element_text(size=14),
legend.title = element_blank(),
axis.title.x = element_blank()) +
annotation_custom(g, xmin=.7, xmax=1.3, ymin=35, ymax=40) + ylim(0,40)
这可能不是原因的解释,而是可行的解决方案。
library(tidyverse)
library(reshape2)
library(grid)
library(png)
df = tibble(
x = c(5,17,31,9,17,10,30,28,16,29,14,34),
y = c(39,6,3,4,1,6,7,2,9,10,3,4),
day = c("d1","cd2","d3","d4","d5","d6","d7","d8","d9","d10","d11","d12")
) %>% mutate(
day = day %>% fct_reorder(x)
) %>% pivot_longer(cols=1:2, names_to = "variable", values_to = "value")
img <- readPNG("imgs/unnamed.png")
g <- rasterGrob(img, interpolate=TRUE)
barcolors <- c("#428953", "#CE2929", "#A3DD57", "#77E599", "#5675D6", "#65ECEF", "#FF8B07", "#D0B100", "#636363", "#D0B100", "#CE2929", "#428953")
df %>% ggplot(aes(x=day, y=value, alpha=variable)) +
geom_bar(aes(fill=day), stat="identity", width = .75, position = position_dodge(width=.8))+
annotation_custom(g, xmin=.7, xmax=1.3, ymin=35, ymax=40)+
annotation_custom(g, xmin=.7, xmax=1.3, ymin=35, ymax=40)+
scale_alpha_manual(values=c(1, .45)) +
scale_fill_manual(values= barcolors) +
theme_bw() +
theme( panel.grid.major.x = element_blank(),
legend.position="none",
legend.text=element_text(size=14),
legend.title = element_blank(),
axis.title.x = element_blank()) +
ylim(0,40)
以下是 pdf 文件的摘录。