Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 在圆形条形图(ggplot2)的中心设置图例_R_Ggplot2 - Fatal编程技术网

R 在圆形条形图(ggplot2)的中心设置图例

R 在圆形条形图(ggplot2)的中心设置图例,r,ggplot2,R,Ggplot2,我正在使用本教程创建圆形条形图: 我想在BrPoice中间创建一个传说,白色区域在那里。然而,到目前为止,我只能在中间添加文本。如果我想画一个带有颜色的小立方体,它会把自己包裹起来,我不知道它是如何使用坐标的(我尝试了x=0,y=0,结果如下,它们总是弯曲的) 以下是我添加的两行内容: geom_tile(aes(x = 1, y = 0, colour = "#EB5500"), width = 100, height = 100, inherit.aes = F) + geom_text(x

我正在使用本教程创建圆形条形图:

我想在BrPoice中间创建一个传说,白色区域在那里。然而,到目前为止,我只能在中间添加文本。如果我想画一个带有颜色的小立方体,它会把自己包裹起来,我不知道它是如何使用坐标的(我尝试了x=0,y=0,结果如下,它们总是弯曲的)

以下是我添加的两行内容:

geom_tile(aes(x = 1, y = 0, colour = "#EB5500"), width = 100, height = 100, inherit.aes = F) +
geom_text(x = 0, aes(y = -100, label = "test"), size = 4)
因此,完整代码现在如下所示:

# Clear workspace 
rm(list = ls())

# Libraries
library(tidyverse)

# Create dataset
data <- data.frame(
  id=seq(1,60),
  individual=paste( "Mister ", seq(1,60), sep=""),
  value=sample( seq(10,100), 60, replace=T)
)

# Make the plot
p <- ggplot(data, aes(x=as.factor(id), y=value)) +       # Note that id is a factor. If x is numeric, there is some space between the first bar

  # This add the bars with a blue color
  geom_bar(stat="identity", fill=alpha("blue", 0.3)) +

  # Limits of the plot = very important. The negative value controls the size of the inner circle, the positive one is useful to add size over each bar
  ylim(-100,120) +

  # Custom the theme: no axis title and no cartesian grid
  theme_minimal() +
  theme(
    axis.text = element_blank(),
    axis.title = element_blank(),
    panel.grid = element_blank(),
    plot.margin = unit(rep(-2,4), "cm")     # This remove unnecessary margin around plot
  ) +

  # This makes the coordinate polar instead of cartesian.
  coord_polar(start = 0) +
  geom_tile(aes(x = 1, y = 0, colour = "#EB5500"), width = 100, height = 100, inherit.aes = F) +
  geom_text(x = 0, aes(y = -100, label = "test"), size = 4)

p
#清除工作区
rm(list=ls())
#图书馆
图书馆(tidyverse)
#创建数据集

数据
主题(legend.position=c(.5.5))
将图例放在绘图的中心。

关于您的问题,我没有真正了解的是图例中应该包含什么。图例的概念是,它们解释了映射(aes()中的某些内容),因此您通常希望在数据中已经包含该映射:

library(tidyverse)

data <- data.frame(
  id=seq(1,60),
  individual=paste( "Mister ", seq(1,60), sep=""),
  value=sample( seq(10,100), 60, replace=T),
  colour = "test1" # added to have something to map to
)

我选择了
test1
来显示任何东西都可以进入数据。要更改颜色,必须定义手动(或其他)刻度:


图例究竟应该显示什么?条形图的映射。你的回答很完美。我以前不明白legend.position()是如何工作的,我想我必须输入坐标。非常感谢。如果您有新问题,请单击按钮提问。如果此问题有助于提供上下文,请包含指向此问题的链接-
p <- ggplot(data, aes(x=as.factor(id), y=value, fill = colour)) +       # Note that id is a factor. If x is numeric, there is some space between the first bar
  geom_bar(stat="identity") +
  ylim(-100,120) +
  theme_minimal() +
  theme(
    axis.text = element_blank(),
    axis.title = element_blank(),
    panel.grid = element_blank(),
    legend.position = c(0.5, 0.5), # move legend to the center
    plot.margin = unit(rep(-2,4), "cm")     # This remove unnecessary margin around plot
  ) +
  coord_polar(start = 0)

p
p +
  scale_fill_manual(values = alpha("blue", 0.3))