Skip to content

control over discrete_scale when faceting #4180

Closed
@ignaczzs

Description

@ignaczzs

Hi Everyone,
I am trying to plot points to a graph from two dataframes (structured identically) and also faceting the graph. I would like to be in full control over the order of how the points are plotted, the axis labels of what I am plotting and which axis labels appear under which panel of the facet. It seems I can only really control two of these three things simultaneously: both options break and limits in the discrete_scale command cause various issues.
My hunch is that on the variable that I am plotting, in one of dataframes one value of the variable is not present and this causes the confusion/problem.
Hopefully my comments reprex will give you a clear idea of the problem.
Thanks in advance for your response,
Zsófia

### Package Load
library(ggplot2)

### Mock Data.framaes
some.df<-data.frame(varname=c("one","two","three"),
                    b=c(0.5,0.6,0.55),
                    vartyp=c("odd","even","odd"),
                    modname=c("some","some","some"))
other.df<-data.frame(varname=c("one","two","three","four"),
                     b=c(-0.5,0.6,-0.55,-1),
                     vartyp=c("odd","even","odd","even"),
                     modname=c("other","other","other","other"))

## Creating Factor variables (relevant later)
some.df$varnameFACTOR<-factor(some.df$varname, levels=c("one","two","three","four"))
other.df$varnameFACTOR<-factor(other.df$varname, levels=c("one","two","three","four"))

### Aspects that I would like to bring together (and are desired to have control over all of this)
## a. Control of order of shapes/points: "other" over "some"
## b. Control of order of axis labels: higher numbers higher on the axis
## c. Control of axis labels by panels: only labels with valid data showing in panel

## Personal hack: this graph is the one I want to achieve in the end (see list of aspects) - but could never get it without the doubling of geom_point for other.df
ggplot()+
  geom_point(data=other.df, aes(x=varnameFACTOR,y=b,color=modname,shape=modname), size=4)+
  geom_point(data=some.df, aes(x=varnameFACTOR,y=b,color=modname,shape=modname),size=3)+
  geom_point(data=other.df, aes(x=varnameFACTOR,y=b,color=modname,shape=modname), size=4)+
  coord_flip(clip = "off")+
  facet_grid(rows=vars(vartyp),scales="free",switch = "y")+
  scale_x_discrete(name="", breaks=c("one","two","three","four"))

### Different versions of the graphs (with comments, of why this is not what I want)
## Varname as string

# Default graph
# a. Control of order of shapes/points: correct,
# b. Control of order of axis labels: Labels are not ordered according to "limits" option, just simply alphabetically
# c. Control of axis labels by panels: correct
ggplot()+
  geom_point(data=other.df, aes(x=varname,y=b,color=modname,shape=modname), size=4)+
  geom_point(data=some.df, aes(x=varname,y=b,color=modname,shape=modname),size=3)+
  coord_flip(clip = "off")+
  facet_grid(rows=vars(vartyp),scales="free",switch = "y")

## Graph: scale_x_discrete+limits
# a. Control of order of shapes/points: correct,
# b. Control of order of axis labels: correct
# c. Control of axis labels by panels: Labels in panels when there is no valid information
ggplot()+
  geom_point(data=other.df, aes(x=varname,y=b,color=modname,shape=modname), size=4)+
  geom_point(data=some.df, aes(x=varname,y=b,color=modname,shape=modname),size=3)+
  coord_flip(clip = "off")+
  facet_grid(rows=vars(vartyp),scales="free",switch = "y")+
  scale_x_discrete(name="", limits=c("one","two","three","four"))

## Graph: scale_x_discrete+breaks
# a. Control of order of shapes/points: correct,
# b. Control of order of axis labels: Labels are not ordered according to "limits" option, just simply alphabetically
# c. Control of axis labels by panels: correct
ggplot()+
  geom_point(data=some.df, aes(x=varname,y=b,color=modname,shape=modname),size=3)+
  geom_point(data=other.df, aes(x=varname,y=b,color=modname,shape=modname), size=4)+
  coord_flip(clip = "off")+
  facet_grid(rows=vars(vartyp),scales="free",switch = "y")+
  scale_x_discrete(name="", breaks=c("one","two","three","four"))

## Graph: scale_x_discrete+breaks+flipped around order of geom_points
# a. Control of order of shapes/points: wrong order
# b. Control of order of axis labels: Labels are not ordered according to "limits" option, just simply alphabetically
# c. Control of axis labels by panels: correct
ggplot()+
  geom_point(data=other.df, aes(x=varname,y=b,color=modname,shape=modname), size=4)+
  geom_point(data=some.df, aes(x=varname,y=b,color=modname,shape=modname),size=3)+
  coord_flip(clip = "off")+
  facet_grid(rows=vars(vartyp),scales="free",switch = "y")+
  scale_x_discrete(name="", breaks=c("one","two","three","four"))

### Varname as factor (Using a factor variable brings us closer to the solution - but is still not the solution)
# a. Control of order of shapes/points: wrong order
# b. Control of order of axis labels: Labels are not ordered according to "limits" option, just simply alphabetically
# c. Control of axis labels by panels: correct
ggplot()+
  geom_point(data=some.df, aes(x=varnameFACTOR,y=b,color=modname,shape=modname),size=3)+
  geom_point(data=other.df, aes(x=varnameFACTOR,y=b,color=modname,shape=modname), size=4)+
  coord_flip(clip = "off")+
  facet_grid(rows=vars(vartyp),scales="free",switch = "y")

## Graph: scale_x_discrete+limits
# a. Control of order of shapes/points: correct,
# b. Control of order of axis labels: correct
# c. Control of axis labels by panels: Labels in panels when there is no valid information
ggplot()+
  geom_point(data=some.df, aes(x=varnameFACTOR,y=b,color=modname,shape=modname),size=3)+
  geom_point(data=other.df, aes(x=varnameFACTOR,y=b,color=modname,shape=modname), size=4)+
  coord_flip(clip = "off")+
  facet_grid(rows=vars(vartyp),scales="free",switch = "y")+
  scale_x_discrete(name="", limits=c("one","two","three","four"))

## Graph: scale_x_discrete+breaks
# a. Control of order of shapes/points: wrong order
# b. Control of order of axis labels: Labels are not ordered according to "limits" option, just simply alphabetically
# c. Control of axis labels by panels: correct
ggplot()+
  geom_point(data=some.df, aes(x=varnameFACTOR,y=b,color=modname,shape=modname),size=3)+
  geom_point(data=other.df, aes(x=varnameFACTOR,y=b,color=modname,shape=modname), size=4)+
  coord_flip(clip = "off")+
  facet_grid(rows=vars(vartyp),scales="free",switch = "y")+
  scale_x_discrete(name="", breaks=c("one","two","three","four"))

## Graph: scale_x_discrete+breaks+flipped around order of geom_points
# a. Control of order of shapes/points: wrong order
# b. Control of order of axis labels: correct
# c. Control of axis labels by panels: correct
ggplot()+
geom_point(data=other.df, aes(x=varnameFACTOR,y=b,color=modname,shape=modname), size=4)+
  geom_point(data=some.df, aes(x=varnameFACTOR,y=b,color=modname,shape=modname),size=3)+
  coord_flip(clip = "off")+
  facet_grid(rows=vars(vartyp),scales="free",switch = "y")+
  scale_x_discrete(name="", breaks=c("one","two","three","four"))

Created on 2020-08-31 by the reprex package (v0.3.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugan unexpected problem or unintended behaviorscales 🐍

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions