Skip to content

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

@hope-data-science

Description

@hope-data-science

学生作品,供参考:

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()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions