问题
我想将情节的图例放在情节上方。我还想在图例键标签(图例文本)上方放置图例键符号(彩色正方形)。不幸的是,当我这样做时,图例键符号会“拉伸”以适合标签的大小。我以为ggplot2可以正常工作,但是如何手动覆盖此功能?
如何在顶部使用可变长度的标签使图例键符号保持一致?
可复制示例
(这不一定是一个最小的示例,以防万一我的实际代码结构(如#include <LiquidCrystal.h>
// Arduino pins number
const int SW_pin = 2; // digital pin connected to switch output
const int X_pin = 0; // analog pin connected to X output
const int Y_pin = 1; // analog pin connected to Y output
const int LCD_RS = 7;
const int LCD_Enable = 8;
const int LCD_D4 = 9;
const int LCD_D5 = 10;
const int LCD_D6 = 11;
const int LCD_D7 = 12;
LiquidCrystal lcd(LCD_RS, LCD_Enable, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
// Basic vars
int none = 0;
String Apps[3] = {"App select","Credits","Test your brain"};
int CurrentApp = 0;
int Yaxis = 1;
int Xaxis = 1;
int HiCh = 0;
int button;
int JXaxis;
int JYaxis;
int MaxHi;
void AppSelect() { // APPSELECT
lcd.setCursor(0,0);
lcd.print("App select menu");
lcd.setCursor(0,1);
lcd.print(Apps[HiCh]);
if (button == 0) {
SelectApp();
}
if (JYaxis <= 2) {
if (HiCh != 0) {
HiCh = HiCh - 1;
lcd.clear();
lcd.setCursor(0,1);
lcd.print(Apps[HiCh]);
delay(300);
}
}
if (JYaxis >= 7) {
if (HiCh != 1) {
HiCh = HiCh + 1;
lcd.clear();
lcd.setCursor(0,1);
lcd.print(Apps[HiCh]);
delay(300);
}
}
}
void Credits() { // CREDITS
MaxHi = 0;
Serial.print("- Credits app loading \n");
lcd.clear();
lcd.setCursor(9,0);
lcd.print("Credits");
lcd.setCursor(0,1);
lcd.print("Made by Alexandre Bergeron");
Serial.print("- Credits app loaded \n");
delay(1300);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(600);
lcd.scrollDisplayLeft();
delay(1500);
CurrentApp = 0;
lcd.clear();
}
void BrainTest() { // BRAINTEST
MaxHi = 1;
HiCh = 0;
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Are you ready?");
lcd.setCursor(0,1);
lcd.print("Yes");
for (;;) {
button = digitalRead(SW_pin);
if (HiCh == 0) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Are you ready?");
lcd.setCursor(0,1);
lcd.print("Yes");
if (button == 0) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Let's start");
delay (1400);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("done");
}
}
if (HiCh == 1) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Are you ready?");
lcd.setCursor(0,1);
lcd.print("No");
if (button == 0) {
CurrentApp = 0;
}
}
break;
}
}
void setup() { // SETUP
Serial.begin(9600);
Serial.print("[2J");
Serial.print(" Serial Monitor opened \n \n");
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
Serial.print("- App selector menu \n");
pinMode(SW_pin, INPUT);
digitalWrite(SW_pin, HIGH);
}
void SelectApp() { // SELECTAPP
switch (HiCh) {
case (0):
CurrentApp = 0;
AppSelect();
break;
case (1):
CurrentApp = 1;
Credits();
break;
case (2):
CurrentApp = 2;
BrainTest();
break;
default:
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Error");
Serial.print("- App loading error \n");
break;
}
}
void loop() { // LOOP
Serial.print(HiCh);
button = digitalRead(SW_pin);
int JYaxis = analogRead(Y_pin) / 128;
int JXaxis = analogRead(X_pin) / 128;
if (JYaxis >= 7) {
if (HiCh != 0) {
HiCh = HiCh - 1;
delay(300);
}
}
if (JYaxis <= 2) {
if (HiCh != MaxHi) {
HiCh = HiCh + 1;
delay(300);
}
}
if (CurrentApp == 0) {
MaxHi = 2;
lcd.setCursor(0,0);
lcd.print("App select menu");
lcd.setCursor(0,1);
lcd.print(Apps[HiCh]);
if (button == 0) {
while (button == 0) {
delay (200);
break;
}
SelectApp();
}
if (JYaxis >= 7) {
if (HiCh != 0) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("App select menu");
lcd.setCursor(0,1);
lcd.print(Apps[HiCh]);
delay(300);
}
}
if (JYaxis <= 2) {
if (HiCh != 2) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("App select menu");
lcd.setCursor(0,1);
lcd.print(Apps[HiCh]);
delay(300);
}
}
}
}
和coord_flip
调用会产生影响)
fill
请注意每个图例符号的大小如何不同,具体取决于标签的长度。
我已经尝试过的东西
我认为它与指南有关,但是我似乎无法正确地做到这一点。使用上图(library(dplyr)
library(ggplot2)
dataFrame <- diamonds %>%
group_by(color, cut) %>%
summarise(count = n()) %>%
group_by(color) %>%
mutate(percent = count/sum(count),
pretty_label = paste0(round(percent*100, 1), "%")) %>%
ungroup()
p <- ggplot(data = dataFrame, mapping = aes(x=color, y = percent, group = cut))+
geom_bar(aes(fill = cut), stat = "identity", position = "fill")+
geom_text(aes(label = pretty_label), position=position_fill(vjust=0.5), colour="white", stat = "identity")+
coord_flip()+
theme(legend.position="top")+
guides(fill = guide_legend(label.position = "bottom", reverse = TRUE))
plot(p)
),我尝试了以下操作:
来自here:p + guides(colour = guide_legend(override.aes = list(size=3)))
和
p + guides(colour = guide_legend(keywidth = .5, keyheight = .5))
来自here :(尝试包装标签)p + guides(colour = guide_legend(keywidth = unit(.5, "cm"), keyheight = unit(.5, "cm")))
我只是因为其他原因尝试了一些不太合乎逻辑的尝试。没有一个起作用。
最终想法
我可能很难知道要搜索什么。如果您能够指出正确的方向,我总是愿意亲自解决问题。任何其他资源都值得欢迎。
先谢谢了!
会话输出
p + guides(color = guide_legend(nrow = 2))
答案 0 :(得分:3)
我不知道是否有一种方法可以独立于文本控制图例颜色框的宽度(除了破解图例杂项之外)。但是,如果您在主题声明中添加runtime: php
env: flex
api_version: 1
handlers:
- url: /.*
script: public/index.php
runtime_config:
document_root: public
beta_settings:
# for Cloud SQL, set this value to the Cloud SQL connection name,
# e.g. "project:region:cloudsql-instance"
cloud_sql_instances: "<project>:<region>:<sql-instance>"
,则所有颜色框都将扩展为相同的宽度(您可能需要稍微向上或向下调整1.5以获得所需的框宽)。>
legend.key.width=unit(1.5, "cm")
您可以通过在两行上放置library(scales)
ggplot(dataFrame, aes(x=color, y = percent))+
geom_bar(aes(fill = cut), stat = "identity") +
geom_text(aes(label = pretty_label), position=position_fill(vjust=0.5),
colour="white", size=3)+
coord_flip()+
theme(legend.position="top",
legend.key.width=unit(1.5, "cm"))+
guides(fill = guide_legend(label.position = "bottom", reverse = TRUE)) +
scale_y_continuous(labels=percent)
来节省一些空间:
Very Good