Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.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,我想移动绘图左侧的向导,将其移近绘图,我在theme中尝试了legend.justification=c()函数,但它不起作用 我在末尾添加了我的行和数据的结构,我将数据分配给“plot_frame”: 我的ggplt代码: legend1 <- ggplot(plot_frame, aes(Var2, Var1)) + geom_point(data = data.frame(Var2 = 1:4, Var1 = 0, value = 0), size = 5,

我想移动绘图左侧的向导,将其移近绘图,我在theme中尝试了legend.justification=c()函数,但它不起作用

我在末尾添加了我的行和数据的结构,我将数据分配给“plot_frame”:

我的ggplt代码:

legend1 <- ggplot(plot_frame, aes(Var2, Var1)) +
  geom_point(data = data.frame(Var2 = 1:4, Var1 = 0, value = 0), size = 5,
           aes(color = factor(Var2))) +
  geom_point(data = data.frame(Var2 = 1:4, Var1 = 0, value = 1), alpha = 0) +
  scale_color_manual(values = c("black", "forestgreen", "#DE2D29", "#3C57A8"),
                     labels = c(expression(
                         CD44^{lo}~"T Cells",
                         CD44^{hi}~CD69^{lo}~"T Cells",
                         CD44^{hi}~CD69^{hi}~CD103^{lo}~"T Cells",
                         CD44^{hi}~CD69^{hi}~CD103^{hi}~"T Cells")),
                     guide = guide_legend(override.aes = list(size = 3), 
                                          label.hjust = 0)) + 
theme (panel.grid = element_blank(), 
       axis.text.y.right = element_text(margin = margin(l = unit(-5, "cm"))),
       axis.text.y = element_text(face="italic", size=5, 
                                 color="black"),
       legend.justification = c(-0.5, 0),
       legend.direction = "vertical",
       legend.key = element_blank(),
       legend.title = element_blank(),
       axis.text.x = element_blank())

我的主要情节和图例代码


library(repr, warn.conflicts = FALSE)
options(repr.plot.width=3.5, repr.plot.height=8.5)


zmapp023 <- ggplot(plot_frame, aes(Var2, Var1, fill = value)) + 
#generate tiles for heatmap
  geom_tile(color = "white", position = position_dodge(), show.legend = TRUE) +

  geom_point(data = data.frame(Var2 = 1:4, Var1 = -1, value = 0), size = 5,
           aes(color = factor(Var2))) +
  geom_point(data = data.frame(Var2 = 1:4, Var1 = 0, value = 1), alpha = 0) +

  scale_color_manual(values = c("black", "forestgreen", "#DE2D29", "#3C57A8"),
                     labels = c(expression(
                         CD44^{lo}~"T Cells",
                         CD44^{hi}~CD69^{lo}~"T Cells",
                         CD44^{hi}~CD69^{hi}~CD103^{lo}~"T Cells",
                         CD44^{hi}~CD69^{hi}~CD103^{hi}~"T Cells")),
                     guide = guide_legend(override.aes = list(fill = NA), 
                                          label.hjust = 0, position="bottom", size = 5)) +

  scale_y_discrete(position = "right") +
  labs(y = "", fill = "", color = " ", x = "")  +


  scale_fill_gradientn(colors = c("#3C57A8", "white", "#DE2D29"),
                       breaks = c(1.5, 0, -1.5),
                       labels = c("1.0", "0", "-1.0"),
                       limits = c(-1.5, 1.5),
                       space = "Lab",
                       guide = "colourbar",
                       aesthetics = "fill") +
  theme_minimal() + guides(colour=FALSE) +


theme (panel.grid = element_blank(), 
       axis.text.y.right = element_text(margin = margin(l = unit(-5, "cm"))),
       axis.text.y = element_text(face=4L, size=7, 
                                 color="black"),

       legend.justification = c(-0.9, 0),
       legend.direction = "vertical",
       legend.key.size = unit(0.6, "cm"),
       legend.key.width = unit(0.2,"cm"),
       legend.title.align = 0.5,
       axis.text.x = element_blank(),plot.margin = unit(c(1,1,5,1), "lines")) +
guides(
        fill = guide_colourbar(
            title = "Relative gene expression \n (z score)",
            title.position = "right",

            title.theme = element_text(angle = -90, size = 7.5),
            direction = "vertical",
            ticks = FALSE)) +

coord_cartesian(clip = 'off') 

legend1 <- ggplot(plot_frame, aes(Var2, Var1)) +
  geom_point(data = data.frame(Var2 = 1:4, Var1 = 0, value = 0), size = 5,
           aes(color = factor(Var2))) +
  geom_point(data = data.frame(Var2 = 1:4, Var1 = 0, value = 1), alpha = 0) +
  scale_color_manual(values = c("black", "forestgreen", "#DE2D29", "#3C57A8"),
                     labels = c(expression(
                         CD44^{lo}~"T Cells",
                         CD44^{hi}~CD69^{lo}~"T Cells",
                         CD44^{hi}~CD69^{hi}~CD103^{lo}~"T Cells",
                         CD44^{hi}~CD69^{hi}~CD103^{hi}~"T Cells")),
                     guide = guide_legend(override.aes = list(size = 3), 
                                          label.hjust = 0)) + 
theme (panel.grid = element_blank(), 
       axis.text.y.right = element_text(margin = margin(l = unit(-5, "cm"))),
       axis.text.y = element_text(face="italic", size=5, 
                                 color="black"),
       legend.justification = c(-0.5, 0),
       legend.direction = "vertical",
       legend.key = element_blank(),
       legend.spacing = unit(0.5,"cm"),
       legend.title = element_blank(),
       axis.text.x = element_blank())


legend <- cowplot::get_legend(legend1)


zmapp023 + annotation_custom(legend$grobs[[1]], xmin = 6, ymax = -13) + labs(tag = "e") +  theme(plot.tag.position = c(.04, 1.02))


库(repr,warn.conflicts=FALSE)
选项(repr.plot.width=3.5,repr.plot.height=8.5)

zmapp023在四处搜索之后,我发现我可以使用主题()中的另一个参数来调整它:


在四处搜索之后,我发现我可以在theme()中使用另一个参数对其进行调整:


library(repr, warn.conflicts = FALSE)
options(repr.plot.width=3.5, repr.plot.height=8.5)


zmapp023 <- ggplot(plot_frame, aes(Var2, Var1, fill = value)) + 
#generate tiles for heatmap
  geom_tile(color = "white", position = position_dodge(), show.legend = TRUE) +

  geom_point(data = data.frame(Var2 = 1:4, Var1 = -1, value = 0), size = 5,
           aes(color = factor(Var2))) +
  geom_point(data = data.frame(Var2 = 1:4, Var1 = 0, value = 1), alpha = 0) +

  scale_color_manual(values = c("black", "forestgreen", "#DE2D29", "#3C57A8"),
                     labels = c(expression(
                         CD44^{lo}~"T Cells",
                         CD44^{hi}~CD69^{lo}~"T Cells",
                         CD44^{hi}~CD69^{hi}~CD103^{lo}~"T Cells",
                         CD44^{hi}~CD69^{hi}~CD103^{hi}~"T Cells")),
                     guide = guide_legend(override.aes = list(fill = NA), 
                                          label.hjust = 0, position="bottom", size = 5)) +

  scale_y_discrete(position = "right") +
  labs(y = "", fill = "", color = " ", x = "")  +


  scale_fill_gradientn(colors = c("#3C57A8", "white", "#DE2D29"),
                       breaks = c(1.5, 0, -1.5),
                       labels = c("1.0", "0", "-1.0"),
                       limits = c(-1.5, 1.5),
                       space = "Lab",
                       guide = "colourbar",
                       aesthetics = "fill") +
  theme_minimal() + guides(colour=FALSE) +


theme (panel.grid = element_blank(), 
       axis.text.y.right = element_text(margin = margin(l = unit(-5, "cm"))),
       axis.text.y = element_text(face=4L, size=7, 
                                 color="black"),

       legend.justification = c(-0.9, 0),
       legend.direction = "vertical",
       legend.key.size = unit(0.6, "cm"),
       legend.key.width = unit(0.2,"cm"),
       legend.title.align = 0.5,
       axis.text.x = element_blank(),plot.margin = unit(c(1,1,5,1), "lines")) +
guides(
        fill = guide_colourbar(
            title = "Relative gene expression \n (z score)",
            title.position = "right",

            title.theme = element_text(angle = -90, size = 7.5),
            direction = "vertical",
            ticks = FALSE)) +

coord_cartesian(clip = 'off') 

legend1 <- ggplot(plot_frame, aes(Var2, Var1)) +
  geom_point(data = data.frame(Var2 = 1:4, Var1 = 0, value = 0), size = 5,
           aes(color = factor(Var2))) +
  geom_point(data = data.frame(Var2 = 1:4, Var1 = 0, value = 1), alpha = 0) +
  scale_color_manual(values = c("black", "forestgreen", "#DE2D29", "#3C57A8"),
                     labels = c(expression(
                         CD44^{lo}~"T Cells",
                         CD44^{hi}~CD69^{lo}~"T Cells",
                         CD44^{hi}~CD69^{hi}~CD103^{lo}~"T Cells",
                         CD44^{hi}~CD69^{hi}~CD103^{hi}~"T Cells")),
                     guide = guide_legend(override.aes = list(size = 3), 
                                          label.hjust = 0)) + 
theme (panel.grid = element_blank(), 
       axis.text.y.right = element_text(margin = margin(l = unit(-5, "cm"))),
       axis.text.y = element_text(face="italic", size=5, 
                                 color="black"),
       legend.justification = c(-0.5, 0),
       legend.direction = "vertical",
       legend.key = element_blank(),
       legend.spacing = unit(0.5,"cm"),
       legend.title = element_blank(),
       axis.text.x = element_blank())


legend <- cowplot::get_legend(legend1)


zmapp023 + annotation_custom(legend$grobs[[1]], xmin = 6, ymax = -13) + labs(tag = "e") +  theme(plot.tag.position = c(.04, 1.02))

legend.position=c(x,y) #where x and y are numeric