ggplot2中的轴样式

huangapple go评论69阅读模式
英文:

axis style in ggplot2

问题

I cannot fulfill your request to translate the code part without providing any other content. If you have any other non-code related text that you would like me to translate, please feel free to provide that.

英文:

I want to apply the axis style in this post

https://stackoverflow.com/questions/10808761/r-style-axes-with-ggplot#

I did well with the y-axis but not with the x-axis because my x-axis was as a factor not a continues
here is the code I use

TKV_İmage <- ggplot(PE_plot5, aes(x = TKV, y = TKV_viscosity)) +
  geom_boxplot(width = 0.7, alpha = 0.8, aes(fill = as.factor(Risk_Group))) +
  scale_y_continuous(name = "Viscosity (cP)", labels = label_number(accuracy = 0.01)) +
  scale_x_discrete(name = "Shear rate (1/s)", labels = c("0.6 RPM", "0.8 RPM", "1.0 RPM", "5.0 RPM", "10.0 RPM", "15.0 RPM", "20.0 RPM", "25.0 RPM", "40.0 RPM", "60.0 RPM")) +
  scale_fill_manual(values = c("#4271AE", "#FF5A5F"), labels = c("Low Risk", "High Risk")) +
  theme_bw() +
  theme(axis.text.x = element_text(colour = "grey20", size = 12, angle = 45, hjust = 1),
        axis.text.y = element_text(colour = "grey20", size = 12, vjust = 0.4),
        axis.title = element_text(size = 14),
        legend.title = element_blank(),
        legend.position = "top",
        legend.text = element_text(size = 12),
        panel.border = element_blank(),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank()) +
        base_breaks_y(PE_plot5$TKV_viscosity)

it's working fine but it doesn't show the X-axis

when I am adding this

 +
base_breaks_x(PE_plot5$factor(TKV))

throws this error

> Error in pretty(x) : attempt to apply non-function

off course because the TKV is a factor I think

any help with this issue?

答案1

得分: 0

以下是您要翻译的内容:

"由于您的 x 轴变量是一个因子,您需要根据您引用的问题来调整代码,通过使用例如 scale_x_discrete 而不是 scale_x_continuous,以及计算 minmax 的分段。另外,我添加了一个 ... 参数,允许传递更多参数,如 namelabelsscale

使用一些虚假的示例数据:

library(ggplot2)
library(scales)

PE_plot5 <- data.frame(
  TKV = c("A", "B", "C"),
  TKV_viscosity = 1:12,
  Risk_Group = rep(c("a", "b"), each = 3)
)

base_breaks_x_discrete <- function(x, ...) {
  b <- factor(x)
  bmin <- min(as.numeric(b))
  bmax <- max(as.numeric(b))
  d <- data.frame(y = -Inf, yend = -Inf, x = bmin, xend = bmax)
  list(
    geom_segment(
      data = d, aes(x = x, y = y, xend = xend, yend = yend),
      inherit.aes = FALSE
    ),
    scale_x_discrete(breaks = levels(b), ...)
  )
}

base_breaks_y <- function(x, ...) {
  b <- pretty(x)
  d <- data.frame(x = -Inf, xend = -Inf, y = min(b), yend = max(b))
  list(
    geom_segment(
      data = d, aes(x = x, y = y, xend = xend, yend = yend),
      inherit.aes = FALSE
    ),
    scale_y_continuous(breaks = b, ...)
  )
}

ggplot(PE_plot5, aes(x = TKV, y = TKV_viscosity)) +
  geom_boxplot(width = 0.7, alpha = 0.8, aes(fill = as.factor(Risk_Group))) +
  scale_fill_manual(
    values = c("#4271AE", "#FF5A5F"),
    labels = c("Low Risk", "High Risk")
  ) +
  theme_bw() +
  theme(
    axis.text.x = element_text(
      colour = "grey20",
      size = 12, angle = 45, hjust = 1
    ),
    axis.text.y = element_text(
      colour = "grey20",
      size = 12, vjust = 0.4
    ),
    axis.title = element_text(size = 14),
    legend.title = element_blank(),
    legend.position = "top",
    legend.text = element_text(size = 12),
    panel.border = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()
  ) +
  base_breaks_y(PE_plot5$TKV_viscosity,
    name = "Viscosity (cP)",
    labels = label_number(accuracy = 0.01)
  ) +
  base_breaks_x_discrete(PE_plot5$TKV,
    name = "Shear rate (1/s)",
    labels = c("0.6 RPM", "0.8 RPM", "1.0 RPM")
  )

ggplot2中的轴样式"

英文:

As your x axis variable is a factor you have to adapt the code from the question you referenced to account for that by using e.g. scale_x_discrete instead of scale_x_continuous as well as the computation of the min and max breaks. Additionally I added a ... argument which allows to pass further arguments like name and labels to the scale.

Using some fake example data:

library(ggplot2)
library(scales)

PE_plot5 &lt;- data.frame(
  TKV = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;),
  TKV_viscosity = 1:12,
  Risk_Group = rep(c(&quot;a&quot;, &quot;b&quot;), each = 3)
)

base_breaks_x_discrete &lt;- function(x, ...) {
  b &lt;- factor(x)
  bmin &lt;- min(as.numeric(b))
  bmax &lt;- max(as.numeric(b))
  d &lt;- data.frame(y = -Inf, yend = -Inf, x = bmin, xend = bmax)
  list(
    geom_segment(
      data = d, aes(x = x, y = y, xend = xend, yend = yend),
      inherit.aes = FALSE
    ),
    scale_x_discrete(breaks = levels(b), ...)
  )
}

base_breaks_y &lt;- function(x, ...) {
  b &lt;- pretty(x)
  d &lt;- data.frame(x = -Inf, xend = -Inf, y = min(b), yend = max(b))
  list(
    geom_segment(
      data = d, aes(x = x, y = y, xend = xend, yend = yend),
      inherit.aes = FALSE
    ),
    scale_y_continuous(breaks = b, ...)
  )
}

ggplot(PE_plot5, aes(x = TKV, y = TKV_viscosity)) +
  geom_boxplot(width = 0.7, alpha = 0.8, aes(fill = as.factor(Risk_Group))) +
  scale_fill_manual(
    values = c(&quot;#4271AE&quot;, &quot;#FF5A5F&quot;),
    labels = c(&quot;Low Risk&quot;, &quot;High Risk&quot;)
  ) +
  theme_bw() +
  theme(
    axis.text.x = element_text(
      colour = &quot;grey20&quot;,
      size = 12, angle = 45, hjust = 1
    ),
    axis.text.y = element_text(
      colour = &quot;grey20&quot;,
      size = 12, vjust = 0.4
    ),
    axis.title = element_text(size = 14),
    legend.title = element_blank(),
    legend.position = &quot;top&quot;,
    legend.text = element_text(size = 12),
    panel.border = element_blank(),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()
  ) +
  base_breaks_y(PE_plot5$TKV_viscosity,
    name = &quot;Viscosity (cP)&quot;,
    labels = label_number(accuracy = 0.01)
  ) +
  base_breaks_x_discrete(PE_plot5$TKV,
    name = &quot;Shear rate (1/s)&quot;,
    labels = c(&quot;0.6 RPM&quot;, &quot;0.8 RPM&quot;, &quot;1.0 RPM&quot;)
  )

ggplot2中的轴样式<!-- -->

huangapple
  • 本文由 发表于 2023年5月10日 23:27:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220217.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定