Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

参考答案新版本B.11 #34

Open
hope-data-science opened this issue Nov 7, 2024 · 3 comments
Open

参考答案新版本B.11 #34

hope-data-science opened this issue Nov 7, 2024 · 3 comments

Comments

@hope-data-science
Copy link

学生作品,供参考:

library(ggplot2)

df=data.frame(
  x = c("a", "b", "c", "d", "e", "f", "g"),
  y = c(6, 5, 4, 3, 2, 2, 1)
)
ggplot(df,aes(x=x,y=y,fill=x))+
  geom_col(show.legend = FALSE)+
  geom_tile(aes(x = as.numeric(factor(x)) - 0.2, y = y  ,fill = x,colour = x), width=0.2, height=0.2,show.legend = FALSE)+
  geom_tile(aes(x = as.numeric(factor(x)) + 0.2, y = y  ,fill = x,colour = x), width=0.2, height=0.2,show.legend = FALSE)+
  scale_fill_manual(
    values = c(
      "a" = "darkgreen", 
      "b" = "gray", 
      "c" = "lightcoral", 
      "d" ="khaki", 
      "e" = "cornflowerblue", 
      "f" = "lightgreen", 
      "g" = "chocolate1"
      )
  )+
  scale_colour_manual(
    values = c(
      "a" = "darkgreen", 
      "b" = "gray", 
      "c" = "lightcoral", 
      "d" ="khaki", 
      "e" = "cornflowerblue", 
      "f" = "lightgreen", 
      "g" = "chocolate1"
    )
  )+
  geom_segment(data=df[df$y > 1,], aes(x = as.numeric(factor(x)) - 0.45, xend = as.numeric(factor(x)) + 0.45, y = 1, yend = 1), color = "black", size = 0.8)+
  geom_segment(data=df[df$y > 2,], aes(x = as.numeric(factor(x)) - 0.45, xend = as.numeric(factor(x)) + 0.45, y = 2, yend = 2), color = "black", size = 0.8)+
  geom_segment(data=df[df$y > 3,], aes(x = as.numeric(factor(x)) - 0.45, xend = as.numeric(factor(x)) + 0.45, y = 3, yend = 3), color = "black", size = 0.8)+
  geom_segment(data=df[df$y > 4,], aes(x = as.numeric(factor(x)) - 0.45, xend = as.numeric(factor(x)) + 0.45, y = 4, yend = 4), color = "black", size = 0.8)+
  geom_segment(data=df[df$y > 5,], aes(x = as.numeric(factor(x)) - 0.45, xend = as.numeric(factor(x)) + 0.45, y = 5, yend = 5), color = "black", size = 0.8)+
  theme_void()
@perlatex
Copy link
Owner

perlatex commented Nov 7, 2024

scale_fill_manual()与 scale_colour_manual() 可以合并,比如

scale_colour_manual(
  aesthetics = c("colour", "fill")
)

其次,多个geom_segment()也可以用purr::map()简化

@hope-data-science
Copy link
Author

可以给一个优化过的版本吗,我一看这个这么多重复,就知道应该有优化空间,但是还没有仔细思量。

@perlatex
Copy link
Owner

perlatex commented Nov 7, 2024

df %>%
  ggplot(aes(x = x, y = y, fill = x)) +
  geom_col(show.legend = FALSE) +
  geom_tile(aes(x = as.numeric(factor(x)) - 0.2, y = y, fill = x, colour = x), width = 0.2, height = 0.2, show.legend = FALSE) +
  geom_tile(aes(x = as.numeric(factor(x)) + 0.2, y = y, fill = x, colour = x), width = 0.2, height = 0.2, show.legend = FALSE) +
  scale_fill_manual(
    values = c(
      "a" = "darkgreen",
      "b" = "gray",
      "c" = "lightcoral",
      "d" = "khaki",
      "e" = "cornflowerblue",
      "f" = "lightgreen",
      "g" = "chocolate1"
    ),
    aesthetics = c("colour", "fill")
  ) +
  map(
    1:5,
    ~ geom_segment(
      data = filter(df, y > .x),
      aes(
        x    = as.numeric(factor(x)) - 0.45,
        y    = .x,
        xend = as.numeric(factor(x)) + 0.45,
        yend = .x
      ),
      color = "black",
      size = 0.8
    )
  ) +
  theme_void()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants