ggplot2中的轴样式

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

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

  1. TKV_İmage <- ggplot(PE_plot5, aes(x = TKV, y = TKV_viscosity)) +
  2. geom_boxplot(width = 0.7, alpha = 0.8, aes(fill = as.factor(Risk_Group))) +
  3. scale_y_continuous(name = "Viscosity (cP)", labels = label_number(accuracy = 0.01)) +
  4. 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")) +
  5. scale_fill_manual(values = c("#4271AE", "#FF5A5F"), labels = c("Low Risk", "High Risk")) +
  6. theme_bw() +
  7. theme(axis.text.x = element_text(colour = "grey20", size = 12, angle = 45, hjust = 1),
  8. axis.text.y = element_text(colour = "grey20", size = 12, vjust = 0.4),
  9. axis.title = element_text(size = 14),
  10. legend.title = element_blank(),
  11. legend.position = "top",
  12. legend.text = element_text(size = 12),
  13. panel.border = element_blank(),
  14. panel.grid.major = element_blank(),
  15. panel.grid.minor = element_blank()) +
  16. base_breaks_y(PE_plot5$TKV_viscosity)

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

when I am adding this

  1. +
  2. 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

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

  1. library(ggplot2)
  2. library(scales)
  3. PE_plot5 <- data.frame(
  4. TKV = c("A", "B", "C"),
  5. TKV_viscosity = 1:12,
  6. Risk_Group = rep(c("a", "b"), each = 3)
  7. )
  8. base_breaks_x_discrete <- function(x, ...) {
  9. b <- factor(x)
  10. bmin <- min(as.numeric(b))
  11. bmax <- max(as.numeric(b))
  12. d <- data.frame(y = -Inf, yend = -Inf, x = bmin, xend = bmax)
  13. list(
  14. geom_segment(
  15. data = d, aes(x = x, y = y, xend = xend, yend = yend),
  16. inherit.aes = FALSE
  17. ),
  18. scale_x_discrete(breaks = levels(b), ...)
  19. )
  20. }
  21. base_breaks_y <- function(x, ...) {
  22. b <- pretty(x)
  23. d <- data.frame(x = -Inf, xend = -Inf, y = min(b), yend = max(b))
  24. list(
  25. geom_segment(
  26. data = d, aes(x = x, y = y, xend = xend, yend = yend),
  27. inherit.aes = FALSE
  28. ),
  29. scale_y_continuous(breaks = b, ...)
  30. )
  31. }
  32. ggplot(PE_plot5, aes(x = TKV, y = TKV_viscosity)) +
  33. geom_boxplot(width = 0.7, alpha = 0.8, aes(fill = as.factor(Risk_Group))) +
  34. scale_fill_manual(
  35. values = c("#4271AE", "#FF5A5F"),
  36. labels = c("Low Risk", "High Risk")
  37. ) +
  38. theme_bw() +
  39. theme(
  40. axis.text.x = element_text(
  41. colour = "grey20",
  42. size = 12, angle = 45, hjust = 1
  43. ),
  44. axis.text.y = element_text(
  45. colour = "grey20",
  46. size = 12, vjust = 0.4
  47. ),
  48. axis.title = element_text(size = 14),
  49. legend.title = element_blank(),
  50. legend.position = "top",
  51. legend.text = element_text(size = 12),
  52. panel.border = element_blank(),
  53. panel.grid.major = element_blank(),
  54. panel.grid.minor = element_blank()
  55. ) +
  56. base_breaks_y(PE_plot5$TKV_viscosity,
  57. name = "Viscosity (cP)",
  58. labels = label_number(accuracy = 0.01)
  59. ) +
  60. base_breaks_x_discrete(PE_plot5$TKV,
  61. name = "Shear rate (1/s)",
  62. labels = c("0.6 RPM", "0.8 RPM", "1.0 RPM")
  63. )

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:

  1. library(ggplot2)
  2. library(scales)
  3. PE_plot5 &lt;- data.frame(
  4. TKV = c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;),
  5. TKV_viscosity = 1:12,
  6. Risk_Group = rep(c(&quot;a&quot;, &quot;b&quot;), each = 3)
  7. )
  8. base_breaks_x_discrete &lt;- function(x, ...) {
  9. b &lt;- factor(x)
  10. bmin &lt;- min(as.numeric(b))
  11. bmax &lt;- max(as.numeric(b))
  12. d &lt;- data.frame(y = -Inf, yend = -Inf, x = bmin, xend = bmax)
  13. list(
  14. geom_segment(
  15. data = d, aes(x = x, y = y, xend = xend, yend = yend),
  16. inherit.aes = FALSE
  17. ),
  18. scale_x_discrete(breaks = levels(b), ...)
  19. )
  20. }
  21. base_breaks_y &lt;- function(x, ...) {
  22. b &lt;- pretty(x)
  23. d &lt;- data.frame(x = -Inf, xend = -Inf, y = min(b), yend = max(b))
  24. list(
  25. geom_segment(
  26. data = d, aes(x = x, y = y, xend = xend, yend = yend),
  27. inherit.aes = FALSE
  28. ),
  29. scale_y_continuous(breaks = b, ...)
  30. )
  31. }
  32. ggplot(PE_plot5, aes(x = TKV, y = TKV_viscosity)) +
  33. geom_boxplot(width = 0.7, alpha = 0.8, aes(fill = as.factor(Risk_Group))) +
  34. scale_fill_manual(
  35. values = c(&quot;#4271AE&quot;, &quot;#FF5A5F&quot;),
  36. labels = c(&quot;Low Risk&quot;, &quot;High Risk&quot;)
  37. ) +
  38. theme_bw() +
  39. theme(
  40. axis.text.x = element_text(
  41. colour = &quot;grey20&quot;,
  42. size = 12, angle = 45, hjust = 1
  43. ),
  44. axis.text.y = element_text(
  45. colour = &quot;grey20&quot;,
  46. size = 12, vjust = 0.4
  47. ),
  48. axis.title = element_text(size = 14),
  49. legend.title = element_blank(),
  50. legend.position = &quot;top&quot;,
  51. legend.text = element_text(size = 12),
  52. panel.border = element_blank(),
  53. panel.grid.major = element_blank(),
  54. panel.grid.minor = element_blank()
  55. ) +
  56. base_breaks_y(PE_plot5$TKV_viscosity,
  57. name = &quot;Viscosity (cP)&quot;,
  58. labels = label_number(accuracy = 0.01)
  59. ) +
  60. base_breaks_x_discrete(PE_plot5$TKV,
  61. name = &quot;Shear rate (1/s)&quot;,
  62. labels = c(&quot;0.6 RPM&quot;, &quot;0.8 RPM&quot;, &quot;1.0 RPM&quot;)
  63. )

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:

确定