ggplot2- not rendering the plot correctly

I'm new to R and started with basic plots. I have used a simple excel table which has the following columns- Family, Family Name, Product.ID, Sales, Time, Value
The data table is similar to the below

|Family|Family.Name|Product.ID|Sales|Time|Value|
|19A| A Box|AA980|ACTUALSALESUNITS|Jan 2021|63|
|19A| A Box|AA980|ACTUALSALESUNITS|Feb 2021|34|
|19A| A Box|AA980|ACTUALSALESUNITS|Mar 2021|93|
|19A| A Box|AA980|ACTUALSALESUNITS|Apr 2021|99|
|19B| B Box|AL345|ACTUALSALESUNITS|Jan 2021|1|
|19B| B Box|AL345|ACTUALSALESUNITS|Feb 2021|0|
|19B| B Box|AL345|ACTUALSALESUNITS|Mar 2021|0|
|19B| B Box|AL345|ACTUALSALESUNITS|Apr 2021|0|

I used the code below but for some reason the when I try to group the chart by Family Name it is just showing a single color and even the legends is not appearing for all values in Family Name

library("ggplot2")
library("data.table")
library("readxl")
Data<-read.xlsx("C:/Users/vc/Desktop/Sales Data.xlsx")
setDT(Data)
Plot<-ggplot(data = Data,aes(x=Time,y=Value,group='Family Name',shape='Family Name',color='Family Name'))+geom_line()+geom_point()

I have attached the output graph and it is not very clear

Could someone please help me on this
Thank you so much

Start with contemplating the following

library(tidyverse)
#economics is a builtin dataset 
(head(economics))
# imagine it has column names with spaces we want to use
economics$`Unemployment Values` <- economics$unemploy
(head(economics))

#original works
ggplot(economics, aes(date, unemploy)) + geom_line()

#incorrect quotes, dont work, as they force a single static 'value'
ggplot(economics, aes(date, "Unemployment Values")) + geom_line()

# correct quoting for non standard variable name is backticks like this
ggplot(economics, aes(date, `Unemployment Values`)) + geom_line()

Thank you so much. Using Backticks work while dealing with fields which has spaces in it. But Still all of the field values are not appearing on the chart.

Its not clear what you mean, can you clarify ?

Since the data contain thirteen values in the family name column and when I try to plot by grouping the family name column. It shows only 6 family name values and didnt include the remaining as it shows the below error
1: The shape palette can deal with a maximum of 6 discrete values because more
than 6 becomes difficult to discriminate; you have 13. Consider specifying
shapes manually if you must have them.
2: Removed 1512 rows containing missing values (geom_point).
So I believe I have to limit the number of values which I try to group, but it would be better if I'd be able to group by all the thirteen values

can you write a list of the 13 shapes you want to use to represent each family name ?
Here is a list to choose from

circle              
circle open         
circle filled       
circle cross        
circle plus         
circle small        
bullet              
square              
square open         
square filled       
square cross        
square plus         
square triangle     
diamond             
diamond open        
diamond filled      
diamond plus        
triangle            
triangle open       
triangle filled     
triangle square     
triangle down open  
triangle down filled
plus                
cross               
asterisk

Could we please use any of these 13 shapes if possible?

you can add another + command after the geom_point for example that assigns shapes like this

... geom_point() +
  scale_shape_manual(
    values =
      c(
        "circle plus", "diamond plus", "circle filled",
        "diamond",  "triangle filled", "asterisk", 
        "circle small", "circle cross", "cross", 
        "circle", "triangle square", "plus","square cross"
      )
  )

Thanks a lot. It does work now :). One question though.. is it possible to assign a specific shape to a specific family value?. For example- A BOX- asterisk shape

 scale_shape_manual(
    values =
      c("A BOX"="asterisk" , 
... etc
1 Like

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.