使用ggradar
时,长变量名不适合窗格。是否可以重塑ggradar
中的变量名?
可复制的示例:
library(ggradar)
suppressPackageStartupMessages(library(dplyr))
library(scales)
data <- data.frame(
group = c("A", "B", "C"),
variable_with_long_name_1 = c(0,1,0.5),
variable_with_long_name_2 = c(0,1,.5),
variable_with_long_name_3 = c(1,0,0.5)
)
ggradar(data)
这有效,看起来像:
有任何提示吗?
答案 0 :(得分:2)
如果可以的话,我建议使用import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
/**
* @see https://stackoverflow.com/questions/2658663
*/
public class TextOverlay extends JPanel {
private BufferedImage image;
private String name;
private String fileX;
private int number;
public TextOverlay(String f, String s, int n) {
name = s;
number = n;
fileX = f;
try {
image = ImageIO.read(new File(fileX));
} catch (IOException e) {
e.printStackTrace();
}
image = process(image, name, number);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, null);
}
private BufferedImage process(BufferedImage old, String name, int number) {
int w = old.getWidth();
int h = old.getHeight();
BufferedImage img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = img.createGraphics();
g2d.drawImage(old, 0, 0, w, h, this);
g2d.setPaint(Color.black);
g2d.setFont(new Font("Franklin Gothic Demi Cond", Font.PLAIN, 30));
String s1 = name;
String s2 = Integer.toString(number);;
FontMetrics fm = g2d.getFontMetrics();
g2d.drawString(s1, 40, 90);
g2d.drawString(s2, 40, 140);
g2d.dispose();
return img;
}
中的Picture:
ggiraphExtra
此外,您将使用library(ggiraphExtra)
g <- ggRadar(data, aes(color = group), scales = "free") +
theme_minimal() +
theme(text = element_text(size=7), # custom font size
axis.text.y = element_blank())
语法。
另外,我认为最好使用简单的ggplot2
,只需要处理尺寸,而不必牺牲文本大小:
ggsave
使用的数据:
g <- ggRadar(data, aes(color = group), scales = "free") +
theme_minimal() +
theme(axis.text.y = element_blank())
print(g)
ggsave("/plt.png", width = 16, height = 9, dpi = 120)
答案 1 :(得分:1)
实际上很简单。 ggradar
允许缩放所有标签:
axis.label.size
选项grid.label.size
选项和 legend.label.size
选项来所以
library(ggradar)
suppressPackageStartupMessages(library(dplyr))
library(scales)
data <- data.frame(
group = c("A", "B", "C"),
variable_with_long_name_1 = c(0,1,0.5),
variable_with_long_name_2 = c(0,1,.5),
variable_with_long_name_3 = c(1,0,0.5)
)
ggradar(data, axis.label.size = 3, grid.label.size = 3, legend.text.size = 10)
尝试类似
答案 2 :(得分:0)
library(ggradar)
suppressPackageStartupMessages(library(dplyr))
library(scales)
data <- data.frame(
group = c("A", "B", "C"),
"variable with long name"= c(0,1,0.5),
"variable with \n long name" = c(0,1,.5),
variable_with_long_name_3 = c(1,0,0.5)
)
ggradar(data)+
ggtitle("Title on \n two lines")
对于其他 ggplot 功能,我在长标签(如标题或名称)中使用了 "\n",但使用 ggradar 它不起作用。也许你仍然可以以此作为改变某些东西的提示
答案 3 :(得分:0)
另一种可能有效的方法,至少如果您的蜘蛛图中只有几个类别:
将文本添加到图表的前面,如以下链接中的 Joachim Schork 所示
https://statisticsglobe.com/add-bold-and-italic-text-to-ggplot2-plot-in-r
ggp + # Add bold text element to plot, could be anything else like italic
annotate("text", x = 4.5, y = 2.2, size = 5,
label = "My Bold Text",
fontface = "bold")