ggplot2 中颜色和形状变量组合成串联图例的问题

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

所以我有一个图,我试图让点由 type1 着色,形状由 note1 等形成。出于某种原因,该图将这些该死的东西连接成这样的键:

type1
note1
- - - - (border value name, 1)
------ (another value name, 1)
* (apple, blue)
O (orange, red)

这是我想要的糟糕图片:

enter image description here

etc,我希望它只有变量的名称,而不是文本中两者的组合/串联。当我使用人工智能聊天机器人时,我得到了一个很好的解决方案,但据我尝试过,该解决方案实际上在我的代码中不起作用。 AI推荐的解决方案(这不起作用):

library(ggplot2)
library(plotly)
library(dplyr)
library(forcats)

# Sample data
set.seed(123)
data <- data.frame(
  groupID = rep(1:10, each = 3),
  AttributeValue = rnorm(30),
  type1 = rep(c("Single Observation", "Multiple Observations"), 15),
  note1 = rep(c("specific time", "shift", "notation", "interesting"), length.out = 30),
  MaterialNumber = sample(1000:9999, 30, replace = TRUE),
  MaterialName = sample(LETTERS, 30, replace = TRUE),
  date = sample(seq(as.Date('2022-01-01'), as.Date('2023-01-01'), by="day"), 30, replace = TRUE)
)

# Create ggplot
p1 <- data %>%
  mutate(groupID = fct_reorder(as.factor(groupID), date)) %>%
  mutate(MaterialInfo = paste(MaterialNumber, "-", MaterialName)) %>%
  ggplot(mapping = aes(x = groupID, y = AttributeValue)) +
  geom_line() + 
  facet_wrap(~ note1, scales = "free", ncol = 2) + 
  labs(x = "groupID") +
  theme_bw() +
  theme(axis.text.x = element_blank(), axis.ticks.x = element_blank(), 
        strip.text.x = element_text(size = 8, vjust = 0.5, margin = margin(0.25, 0, 0.25, 0, "cm"))) +
  geom_point(mapping = aes(
    color = note1,
    shape = type1,
    stroke = ifelse(type1 == "Multiple Observations", 0.5, 1),
    text = paste("SKU:", MaterialInfo,
                 "<br>groupID:", groupID,
                 "<br>date:", date,
                 "<br>Attribute Value:",  AttributeValue)),
    show.legend = TRUE) +
  scale_shape_manual(values = c("Multiple Observations" = 1, "Single Observation" = 16)) +
  scale_color_manual(values = c("specific time" = "#ffc425", 
                                "shift" = "#00b159", 
                                "notation" = "#d11141", 
                                "interesting" = "#f37735")) +
  guides(color = guide_legend(order = 1, override.aes = list(shape = NA)), 
         shape = guide_legend(order = 2))

# Convert to plotly
p1_plotly <- ggplotly(p1, tooltip = "text")

# Display plot
p1_plotly 

但是,当我用我的代码尝试这个时,我仍然无法让它工作:

library(ggplot2)
library(plotly)
library(dplyr)
library(forcats)

# Sample data
set.seed(123)
data <- data.frame(
  groupID = rep(1:10, each = 3),
  AttributeValue = rnorm(30),
  type1 = rep(c("Single Observation", "Multiple Observations"), 15),
  note1 = rep(c("specific time", "shift", "notation", "interesting"), length.out = 30),
  objectNumber = sample(1000:9999, 30, replace = TRUE),
  objectName = sample(LETTERS, 30, replace = TRUE),
  date = sample(seq(as.Date('2022-01-01'), as.Date('2023-01-01'), by="day"), 30, replace = TRUE)
)

# Create ggplot
p1 <- data %>%
  mutate(groupID = fct_reorder(as.factor(groupID), date)) %>%
  mutate(objectInfo = paste(objectNumber, "-", objectName)) %>%
  ggplot(mapping = aes(x = groupID, y = AttributeValue)) +
  geom_line() + 
  facet_wrap(~ note1, scales = "free", ncol = 2) + 
  labs(x = "groupID") +
  theme_bw() +
  theme(axis.text.x = element_blank(), axis.ticks.x = element_blank(), 
        strip.text.x = element_text(size = 8, vjust = 0.5, margin = margin(0.25, 0, 0.25, 0, "cm"))) +
  geom_point(mapping = aes(
    color = note1,
    shape = type1,
    stroke = ifelse(type1 == "Multiple Observations", 0.5, 1),
    text = paste("SKU:", objectInfo,
                 "<br>groupID:", groupID,
                 "<br>date:", date,
                 "<br>Attribute Value:",  AttributeValue)),
    show.legend = TRUE) +
  scale_shape_manual(values = c("Multiple Observations" = 1, "Single Observation" = 16)) +
  scale_color_manual(values = c("specific time" = "#ffc425", 
                                "shift" = "#00b159", 
                                "notation" = "#d11141", 
                                "interesting" = "#f37735")) +
  guides(color = guide_legend(order = 1, override.aes = list(shape = NA)), 
         shape = guide_legend(order = 2))

# Convert to plotly
p1_plotly <- ggplotly(p1, tooltip = "text")

# Display plot
p1_plotly

enter image description here

感谢帮助,谢谢

r ggplot2 plotly legend ggplotly
1个回答
0
投票

仅使用

plot_ly
而不是传递
ggplot
对象可能更容易

library(dplyr)
library(ggplot2)
library(forcats)
library(plotly)

  



clean = data |>
janitor::clean_names() |>
  mutate(group_id = fct_reorder(as.factor(group_id), date),
         material_info = paste(material_number, '-', material_name),
         labs_plot = glue::glue('SKU: {material_info} <br>
                                 groupID: {group_id} <br>
                                 Attribute Value: {attribute_value}'))



pal = c('#ffc425', '#00b159', '#d11141', '#f37735')

pal = setNames(pal, c('specific time', 'shift', 'notation', 'interesting'))


plot_ly(data = clean,
         x = ~group_id,
         y = ~attribute_value,
         text = ~labs_plot) |> 
  add_markers(
              type = 'scatter',
              mode = 'markers',
              symbol = ~type1,
              symbols = c('o', 'circle'),
              color = I('black')) |> 
  add_markers(type = 'scatter',
              mode = 'markers',
              color = ~note1,
              colors = pal)

或者,

ggiraph
可以很好地处理现有的
ggplot
对象,您所要做的就是将其放入
_interactive

library(ggiraph)



p = ggplot(clean, aes(x = group_id, y = attribute_value,
                      color = note1, shape = type1, tooltip = labs_plot)) +
  geom_point_interactive() +
  scale_shape_manual_interactive(values = c("Multiple Observations" = 1, "Single Observation" = 16)) +
  scale_color_manual_interactive(values = c("specific time" = "#ffc425", 
                                            "shift" = "#00b159", 
                                            "notation" = "#d11141", 
                                            "interesting" = "#f37735"))


girafe(ggobj = p)

创建于 2024 年 11 月 26 日,使用 reprex v2.1.1

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