Help with legend

Hello, I am trying to change alphabets "a, a, a" in legend boxes to Uppercase A, F and T, but its not working. If someone can please help me.

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.5.3
library(reprex)
#> Warning: package 'reprex' was built under R version 3.5.3
library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 3.5.3
#> Warning: package 'tibble' was built under R version 3.5.3
#> Warning: package 'tidyr' was built under R version 3.5.3
#> Warning: package 'purrr' was built under R version 3.5.3
#> Warning: package 'dplyr' was built under R version 3.5.3
#> Warning: package 'stringr' was built under R version 3.5.3
library(reshape2)
#> Warning: package 'reshape2' was built under R version 3.5.3
#> 
#> Attaching package: 'reshape2'
#> The following object is masked from 'package:tidyr':
#> 
#>     smiths
library(ggrepel)
#> Warning: package 'ggrepel' was built under R version 3.5.3
mydata <-read.table("F:/CATAFileForR.csv", header = T, sep = ",") 
#help from community
mds <- cmdscale(1-cor(mydata[, -c(1:2,20)], use = "complete.obs")) 
categories <-c("Texture", "Texture", "Texture", "Texture", "Texture",
               "Aroma", "Flavor", "Flavor", "Aroma", "Aroma",
               "Flavor", "Aroma", "Flavor", "Texture")
mds <- cmdscale(1-cor(mydata[, -c(1:2,20)], use = "complete.obs")) 
ggplot(data = as.data.frame(mds), aes(x =mds[,1], y = mds[,2])) + 
  theme_grey() + 
  geom_label_repel(aes(label =  rownames(mds),fill = factor(categories)), size =3.5,
  color = 'white', box.padding = unit(0.45, "lines"), segment.color = 'grey50') + 
  theme(legend.title = element_blank()) + geom_vline(xintercept=c(0), linetype="dashed") +
  geom_hline(yintercept = c(0), linetype="dashed") +
  geom_point()

Created on 2020-06-12 by the reprex package (v0.3.0)

This is not possible. The "a" simply represents that this is a text layer. If you prefer to not show them, add the argument show.legend= FALSE to your geom_label_repel() layer.

A note on your reprex: There is an absolute path in your code thus no one can reproduce your example. Next time please also share the data (e.g. via dput) or use some toy data to illustrate your problem.

1 Like

Thanks @Z3tt for the help. How to use dput, can you please explain. Though i solved my legend problem, but i am still trying to figure out how to close pack legend box space to keys. If you can please help me with that.
Here is my code, still i dont know how to use dput, please excuse:

library(ggplot2)
library(reprex)
library(tidyverse)
library(reshape2)
#> 
#> Attaching package: 'reshape2'
#> The following object is masked from 'package:tidyr':
#> 
#>     smiths
library(ggrepel)
mydata <-read.table("F:/CATAFileForR.csv", header = T, sep = ",") 
#help from community
mds <- cmdscale(1-cor(mydata[, -c(1:2,20)], use = "complete.obs")) 
categories <-c("Texture", "Texture", "Texture", "Texture", "Texture",
               "Aroma", "Flavor", "Flavor", "Aroma", "Aroma",
               "Flavor", "Aroma", "Flavor", "Texture")
mds <- cmdscale(1-cor(mydata[, -c(1:2,20)], use = "complete.obs"))
ggplot(data = as.data.frame(mds), aes(x =mds[,1], y = mds[,2])) + 
  theme_grey() + theme(legend.position = c(.40, .65)) +
  geom_label_repel(aes(label =  rownames(mds),fill = factor(categories)), size =2.3,
  color = 'white', fontface = "bold", box.padding = unit(0.25, "lines"), segment.color = 'grey50') + 
  theme(legend.title = element_blank()) + geom_vline(xintercept=c(0), linetype="dashed") +
  geom_hline(yintercept = c(0), linetype="dashed") + 
  geom_point() + guides(fill = guide_legend(title = "Legend Title",
                                            override.aes = aes(label = "")))

Created on 2020-06-24 by the reprex package (v0.3.0)

You're welcome! The line mydata <-read.table("F:/CATAFileForR.csv", header = T, sep = ",") reads from a local directory, thus we are not asble to run your code.

So there are two ways to ensure reproducibility to help us answer your questions:

(a) Assume you have a dataset called df - just run dput(df) and copy-paste the output in your example (i.e. explicitly assign the vector dput(df) returns to df again).

(b) Better: Use the {reprex} package to create your example. This package allows for simple copy-paste of your code and output while ensuring it can be run independently. Check out the vignette if you want to know more.

EDIT: I just realized you have used the {reprex} package, strange it does not throw an error when reading your local file...

Also, could you please mark the topic as answered by adding my first reply as solution? Thanks!

@Z3tt Thanks for the help. Can you please now check the code for why legend box is not tightly wrapped around legend keys and text. You can copy code from previous response. Here is data achieved through dput:

structure(list(panelist = c(1L, 1L, 1L, 1L, 1L), sample = c("Russian banana",
"Vermillion", "Atlantic", "POR12PG28-3", "Valery"), Mealy_T = c(0L,
0L, 0L, 1L, 1L), Fluffy_T = c(1L, 0L, 1L, 0L, 0L), Smooth_T = c(0L,
0L, 1L, 0L, 0L), Moist_T = c(0L, 1L, 1L, 1L, 1L), Dense_T = c(0L,
1L, 1L, 1L, 0L), Cookedpotato_A = c(0L, 0L, 0L, 0L, 0L), Cookedpotato_F = c(1L,
0L, 1L, 0L, 0L), Metallic_F = c(0L, 1L, 0L, 1L, 1L), Metallic_A = c(1L,
1L, 0L, 1L, 1L), Rawpotato_A = c(0L, 1L, 0L, 0L, 0L), Rawpotato_F = c(0L,
1L, 0L, 0L, 1L), Earthy_A = c(1L, 1L, 0L, 0L, 0L), Earthy_F = c(1L,
1L, 0L, 0L, 0L), Dry_T = c(1L, 0L, 0L, 0L, 0L)), row.names = c(NA,
5L), class = "data.frame")

Without running your code I can tell you why: it is the default. You need to set the legend.margin within the theme to your needs, e.g.

g + theme(legend.margin = margin(0, 0, 0, 0))

So I had a look at your code and here are some comments. Your ggplot code is quite messy and I would highly recommend to sort it which makes it way more easier to changfe things and find errors.
It is also not a good routine to refer to the aesthetics (e.g. x, y and label) in this way. Also, while I am now able to have a look at your data, I can't run the cmdscale line so I have used dummy names in the aesthetics in the cleaner version below:

Error:

mds <- cmdscale(1-cor(mydata[, -c(1:2,20)], use = "complete.obs")) 
Error in cmdscale(1 - cor(mydata[, -c(1:2, 20)], use = "complete.obs")) : 
  NA values not allowed in 'd'
In addition: Warning message:
In cor(mydata[, -c(1:2, 20)], use = "complete.obs") :
  the standard deviation is zero

New code:

## turn data into dataframe first and modify parameters:
## add the rowname columns (to-do) and turn categories into factor
df <- as_tibble(mds) %>% 
  mutate(
    categories = as_factor(categories),
    id = ...
  )

## final data frame should look like this: "val1 | val2 | categories | id"


## cleaner code: 
## use data frame and refer to cols without [] or $
## combine theme changes into one
## each layer gets a new line
## sort your layers in a logical order
## line breaks to avoid too long lines
ggplot(data = df, aes(x = val1, y = val2)) + 
  geom_label_repel(aes(label =  id, fill = categories), 
                   size = 2.3, color = 'white', 
                   fontface = "bold", 
                   box.padding = unit(0.25, "lines"), 
                   segment.color = 'grey50',
                   show.legend = FALSE) + 
  geom_vline(xintercept = 0, linetype = "dashed") +
  geom_hline(yintercept = 0, linetype = "dashed") + 
  geom_point() +
  theme_grey() + 
  theme(legend.position = c(.40, .65),
        legend.title = element_blank(),
        legend.margin = margin(0, 0, 0, 0))

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.