如何调整和删除交叉表图中的图例标签?

问题描述 投票:0回答:1

[enter image description here基本上,我希望标记为8的点读为“不喜欢”,标记为18的点读为“喜欢”。我想删除图例中的其他4点。我不确定那些人如何到达那里。

我已经尝试过使用scale_fill_discrete()和scale_fill_manual(),但它们在这里不起作用。有提示吗?

  #Load libraries
  library(ggplot2)
  library(dplyr)

  #Specify foods and countries
  foods <- c("cake", "cookies", "chocolate", "brownies")
  nations <- c("[![Belarus][1]][1]", "Zimbabwe", "Tanzania", "Morocco")

  #Create the data frame
  crosstab <- expand.grid(foods, nations)
  crosstab$value <- c(8, 8, 18, 18, 18, 18, 8, 18, 18, 18, 8, 18, 18, 8, 8, 18)

  #Plot the visualization
  final_plot <- ggplot(crosstab, aes(Var1, Var2)) + geom_point(aes(size = value), color = "#3484DA") + xlab("") + ylab("")

  #Add design elements
  final_plot + scale_size_continuous(range = c(5, 10)) +
    scale_x_discrete(position = "top") +
    theme(legend.position = "bottom",
          legend.title = element_blank(),
          legend.spacing.x = unit(0.1, "inches"),
          axis.text.x = element_text(size = 10.5, face = "bold"),
          axis.text.y = element_text(size = 10.5, face = "bold"),
          axis.ticks = element_blank(),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.background = element_blank())
r ggplot2 data-visualization crosstab
1个回答
0
投票

您可以在breaks中添加labelsscale_size_continuous

  final_plot + scale_size_continuous(range = c(5, 10), breaks = c(8,18), labels = c("Disliked","Liked")) +
    scale_x_discrete(position = "top") +
    theme(legend.position = "bottom",
          legend.title = element_blank(),
          legend.spacing.x = unit(0.1, "inches"),
          axis.text.x = element_text(size = 10.5, face = "bold"),
          axis.text.y = element_text(size = 10.5, face = "bold"),
          axis.ticks = element_blank(),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.background = element_blank())

enter image description here

它回答了您的问题吗?

© www.soinside.com 2019 - 2024. All rights reserved.