1

Here is my data:

Food    nutrient1   nutrient2   nutrient1se nutrient2se
Control 50  1   2.5 0.02
D1  100 1   4   0.05
D2  90  0.9 3   0.03
D3  100 0.9 6   0.04

My code is:

 library(ggplot2)
 ggplot (data = d, aes(x= nutrient2, y = nutrient1, group=Food))+
 geom_point (aes (shape=Food, colour=Food), size=4, shape=c(15,16,17,18))+
 geom_errorbarh(aes(xmin=nutrient2-nutrient2se, xmax=nutrient2+nutrient2se), length=0.2, colour="orange")+
 geom_errorbar(aes(ymin= nutrient1-nutrient1se, ymax= nutrient1 +nutrient1se), width=0.2, colour="orange")+
= nutrient1-nutrient1se, ymax= nutrient1 +nutrient1se), width=0.2, colour="orange")+
 scale_size_area()+
 xlim(0, 1.5)+
 ylim(90, 120)+
 xlab("Total nutrient2 (g)") +
 ylab("Total nutrient1 (g)") +
 theme_update(plot.title = element_text(hjust = 0.5))+
 theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
 panel.background = element_blank(), axis.line = element_line(colour = "orange"))+
 guides(color=guide_legend(override.aes=list(fill=NA)))+ 
plot.background=element_rect(fill="transparent",colour=NA),
legend.key = element_rect(fill = "transparent", colour =   "transparent"))+
theme(axis.title.x = element_text(colour = "orange"),axis.title.y = element_text(colour = "orange"))

I need to match the legend's shapes with the points in the grouped scatter plot, could you please help me? Thanks

mnm
  • 1,962
  • 4
  • 19
  • 46
Sally
  • 17
  • 3
  • This code does not run, e.g. `geom_errorbar(aes(ymin= nutrient1-nutrient1se, ymax= nutrient1 +nutrient1se), width=0.2, colour="orange")+ = nutrient1-nutrient1se, ymax= nutrient1 +nutrient1se), width=0.2, colour="orange")+` is missing a few words in the middle. Can you please update that, and clarify your question? – Jon Spring Sep 26 '18 at 13:46
  • @Sally, suggest to use `dput` to show the data. See [here](https://stackoverflow.com/questions/49994249/example-of-using-dput) on how to use it. In short, wrap your dataframe in dput. Example, `dput(iris)`, press enter key. Then copy paste the output structure here. This is easier for people to help you. – mnm Sep 26 '18 at 13:46

1 Answers1

1

(Please note, the original code had some typos. Please try to provide working code so that it will be easier for people to help.)

It looks like the problem is that you both "set" and "mapped" shape for your geom_points.

https://www.superdatascience.com/ggplot2-setting-vs-mapping-aesthetics/

When you "map" by using the aes() function, ggplot adds a legend. When you "set", you are manually setting the appearance of your chart elements and ggplot will not add a legend. If you want to "map" the aesthetics, but override the defaults and make specific choices for how your values will get mapped, you can use aes() and later use scale_manual_* to specify your choices.

https://ggplot2.tidyverse.org/reference/scale_manual.html

Here are some edits to your code that might be closer to what you're looking for.

d <- read.table(header = T, text = "
    Food    nutrient1   nutrient2   nutrient1se nutrient2se
    Control 50  1   2.5 0.02
    D1  100 1   4   0.05
    D2  90  0.9 3   0.03
    D3  100 0.9 6   0.04")

library(ggplot2)
ggplot (data = d, aes(x= nutrient2, y = nutrient1, group=Food))+
  geom_point (aes (shape=Food, colour=Food), size=4) +
    # Note, above line originally ended "shape=c(15,16,17,18))+"
  geom_errorbarh(aes(xmin=nutrient2-nutrient2se, 
                     xmax=nutrient2+nutrient2se), 
                 # length=0.2, 
                 colour="orange")+
  geom_errorbar(aes(ymin= nutrient1-nutrient1se, 
                    ymax= nutrient1 +nutrient1se), 
                width=0.2, colour="orange")+
  # The code below is missing a geom_* 
  # = nutrient1-nutrient1se, ymax= nutrient1 +nutrient1se), width=0.2, colour="orange")+  
  scale_size_area()+
  scale_shape_manual(values = c(15,16,17,18)) +
  xlim(0, 1.5)+
  ylim(90, 120)+
  xlab("Total nutrient2 (g)") +
  ylab("Total nutrient1 (g)") +
  # Reordered this out of the theme() function
  guides(color=guide_legend(override.aes=list(fill=NA)))+ 
  theme_update(plot.title = element_text(hjust = 0.5)) +
  theme(panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank(),
        panel.background = element_blank(), 
        axis.line = element_line(colour = "orange"),
        plot.background=element_rect(fill="transparent",colour=NA),
        legend.key = element_rect(fill = "transparent", 
                                  colour =   "transparent"))+
  theme(axis.title.x = element_text(colour = "orange"),
        axis.title.y = element_text(colour = "orange"))

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53
  • Thank you so much for your great help in editing my code. – Sally Sep 26 '18 at 22:03
  • It gives this error when I run it: Error: Cannot add ggproto objects together. Did you forget to add this object to a ggplot object? – Sally Sep 26 '18 at 22:04
  • I had a typo, with a comma at the end of the geom_point line instead of a +. Sorry about that! – Jon Spring Sep 26 '18 at 22:22
  • Thank you! I did changed it into comma but still getting an error, any guess? – Sally Sep 27 '18 at 21:05
  • Yes, I got same error: Error: Cannot add ggproto objects together. Did you forget to add this object to a ggplot object? – Sally Sep 28 '18 at 08:24