在ggplot中并排显示正确图例标签的boxplot

时间:2018-02-03 05:44:47

标签: r ggplot2

我正在创建一个由十六进制颜色着色的并排箱图。我似乎有它工作,但我无法得到传说(在图的右侧)有切割标签(例如:“公平”而不是“#F8766D”)。这是一个模仿我的问题的MWE:

library(ggplot2)
library(dplyr)

df <- diamonds[sample(1:nrow(diamonds), size = 1000),]
colList = scales::hue_pal()(5)
df <- as.data.frame(df)
df <- df %>% select("cut","price")
saveColor <- df %>% transmute(color = plyr::mapvalues(cut, c("Fair", "Good", "Very Good", "Premium", "Ideal"), colList))
df$color <- unlist(saveColor)

ggplot(df, aes(cut, price)) + geom_boxplot(aes(fill = factor(color))) + scale_colour_manual(labels = c("Fair", "Good", "Very Good", "Premium", "Ideal"), values = colList)

enter image description here

1 个答案:

答案 0 :(得分:2)

不确定您正在尝试做什么,但一般情况下,您不会将颜色作为列添加到数据集中。相反,您只需将它们作为颜色标度的值提供。还要注意使代码变得简单多了。

import telebot

bot = telebot.TeleBot("Token")

@bot.message_handler(commands=['start'])
def handle_start(message):
   menu = telebot.types.ReplyKeyboardMarkup(True, False)
   menu.row('Chat')
   menu.row('Not today')

   bot.send_message(message.from_user.id, 'Hi there!', reply_markup=menu)
   position = 0
   return (position)

@bot.message_handler(content_types=['text'])
def handle_text(message):
   if message.text == 'Chat':
      main_menu_1 = telebot.types.ReplyKeyboardMarkup(True, False)
      main_menu_1.row('Good')
      main_menu_1.row('Bad')

      bot.send_message(message.from_user.id, 'How are you today?', reply_markup=main_menu_1)
      position = 1.1
      return (position)

   elif message.text == 'Good':
      sub_menu_1_1 = telebot.types.ReplyKeyboardMarkup(True, False)
      sub_menu_1_1.row('How are you?')
      sub_menu_1_1.row('Bye')

      bot.send_message(message.from_user.id, 'I am glad for you', reply_markup=sub_menu_1_1)
      position = 2.1
      return (position)

   elif message.text == 'Bad':
      sub_menu_1_2 = telebot.types.ReplyKeyboardMarkup(True, False)
      sub_menu_1_2.row('Alright')
      sub_menu_1_2.row('Why?')

      bot.send_message(message.from_user.id, 'I dont care)', reply_markup=sub_menu_1_2)
      position = 2.2
      return (position)

   elif message.text == 'Not today':
      main_menu_2 = telebot.types.ReplyKeyboardMarkup(True, False)
      main_menu_2.row('Not in a mood')
      main_menu_2.row('You are just a machine')

      bot.send_message(message.from_user.id, 'Why not?', reply_markup=main_menu_2)
      position = 1.2
      return (position)

   elif message.text == 'Not in a mood':
      sub_menu_2_1 = telebot.types.ReplyKeyboardMarkup(True, False)
      sub_menu_2_1.row('Its all good')
      sub_menu_2_1.row('See you')

      bot.send_message(message.from_user.id, 'Sorry to hear that(', reply_markup=sub_menu_2_1)
      position = 2.4
      return (position)

   elif message.text == 'You are just a machine':
      sub_menu_2_2 = telebot.types.ReplyKeyboardMarkup(True, False)
      sub_menu_2_2.row('Not funny')
      sub_menu_2_2.row('Funny')

      bot.send_message(message.from_user.id, 'You are just a human', reply_markup=sub_menu_2_2)
      position = 2.5
      return (position)

@bot.message_handler(commands=['back'])
def handle_back(message):
   if position.handle_text == 0:
      bot.send_message(message.from_user.id, 'You are in the main menu already', reply_markup=main_menu)
   elif 1 >= position < 2:
    bot.send_message(message.from_user.id, 'You have returned to the main menu',reply_markup=main_menu)
   elif 2 >= position < 3:
    bot.send_message(message.from_user.id, 'You have returned to a submenu', reply_markup=main_flavor_menu)

bot.polling(none_stop=True)

enter image description here