我最近遇到了幾個ggplot產生鋸齒狀線條的情況。在以下示例中,我使用包生成密集的時間程序資料fda并繪制兩條線圖。第一個圖給出了黑線,另一個圖顯示了相同的線,只是我們使用不同的顏色來表示值的符號。最后,我將繪圖匯出為 eps 檔案并在 Adob??e Illustrator 中打開它們。
# install.packages("fda")
# dir.create("tmp")
library(dplyr)
library(tidyr)
library(ggplot2)
library(fda)
times_sparse <- seq(0, 10, 0.5)
times <- seq(0, 10, 0.02)
basis <- create.bspline.basis(
rangeval = c(0, 10), norder = 4,
breaks = times_sparse
)
nbasis <- basis$nbasis
set.seed(2501)
coeff <- rnorm(nbasis, sd = 0.1)
y <- eval.fd(times, fd(coeff, basis)) |> as.numeric()
dat <- data.frame(t = times, y = y) |>
mutate(pos = factor((y > 0) * 1, levels = c(1, 0)))
### first plot: 1 colors, smooth lines
ggplot(dat)
geom_line(aes(x = t, y = y, color = pos, group = 1))
theme_bw()
theme(panel.grid = element_blank())
# ggsave("tmp/line1a.eps", device = "eps",
# width = 6, height = 6)
### second plot: 2 colors, jagged lines
ggplot(dat)
geom_line(aes(x = t, y = y))
theme_bw()
theme(panel.grid = element_blank())
# ggsave("tmp/line1b.eps", device = "eps",
# width = 6, height = 6)
在顯示放大線的螢屏截圖中,我們觀察到第一個圖中的線是平滑的,而第二個圖中的線是鋸齒狀的。我該如何解決這個問題?

這是我的系統資訊:
# R version 4.1.1 (2021-08-10)
# Platform: x86_64-w64-mingw32/x64 (64-bit)
# Running under: Windows 10 x64 (build 22000)
注意:我的目標是生成一個類似于 R 中第二個圖的 eps/pdf 檔案。感謝其他實作相同目標的方法。
uj5u.com熱心網友回復:
你應該添加lineend = "round"到你的geom_line
ggplot(dat)
geom_line(aes(x = t, y = y, color = pos, group = 1), lineend = "round")
theme_bw()
theme(panel.grid = element_blank())
通過export -> save as PDF和它看起來也不錯windows() -> save as...。
一個例子 (2400%):

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/350425.html
下一篇:Cbind在R中獲取環境物件
