Customizing the angle in a ggplot x-axis tick labels
I want to plot a heatmap
using ggplot2
, and I want to customize the x-axis ticks - location, text, and angle.
Here are my example data:
set.seed(1)
df <- reshape2::melt(matrix(rnorm(100*20),100,20,dimnames = list(paste0("G",1:100),paste0("S",1:20))))
I'd like to have the x-axis tick have only a single tick value, located in the middle of the axis range - 10, with the label: "X-Label", at a 90 degrees angle. In addition, I do not want an x-axis title.
Here's what I'm trying:
library(ggplot2)
ggplot(data=df,mapping=aes(x=Var2,y=Var1,fill=value))+
geom_tile()+theme_minimal()+scale_fill_gradient2(name="Scaled Value",low="darkblue",mid="gray",high="darkred")+
scale_x_discrete(breaks=10,labels="X-Label")+theme(axis.title.x=element_blank(),axis.text.x=element_text(angle=90,hjust=1,vjust=0.5))
So, obviously it's not working.
I thought that I can define the angle as part of the scale_x_discrete
arguments but couldn't find that option.
Also, note that I do not want an x-axis title but only the tick text. So I'm not sure why the breaks
and labels
arguments in scale_x_discrete
are ignored.
Here's the desired outcome using plotly
:
library(plotly)
library(dplyr)
plot_ly(z=c(df$value),x=df$Var2,y=df$Var1,colors=grDevices::colorRamp(c("darkblue","gray","darkred")),type="heatmap",colorbar=list(title="Scaled Value",len=0.4)) %>%
layout(yaxis=list(title=NULL),xaxis=list(tickangle=90,tickvals=10,ticktext="X-Label"))
Thanks
r ggplot2 axis-labels
add a comment |
I want to plot a heatmap
using ggplot2
, and I want to customize the x-axis ticks - location, text, and angle.
Here are my example data:
set.seed(1)
df <- reshape2::melt(matrix(rnorm(100*20),100,20,dimnames = list(paste0("G",1:100),paste0("S",1:20))))
I'd like to have the x-axis tick have only a single tick value, located in the middle of the axis range - 10, with the label: "X-Label", at a 90 degrees angle. In addition, I do not want an x-axis title.
Here's what I'm trying:
library(ggplot2)
ggplot(data=df,mapping=aes(x=Var2,y=Var1,fill=value))+
geom_tile()+theme_minimal()+scale_fill_gradient2(name="Scaled Value",low="darkblue",mid="gray",high="darkred")+
scale_x_discrete(breaks=10,labels="X-Label")+theme(axis.title.x=element_blank(),axis.text.x=element_text(angle=90,hjust=1,vjust=0.5))
So, obviously it's not working.
I thought that I can define the angle as part of the scale_x_discrete
arguments but couldn't find that option.
Also, note that I do not want an x-axis title but only the tick text. So I'm not sure why the breaks
and labels
arguments in scale_x_discrete
are ignored.
Here's the desired outcome using plotly
:
library(plotly)
library(dplyr)
plot_ly(z=c(df$value),x=df$Var2,y=df$Var1,colors=grDevices::colorRamp(c("darkblue","gray","darkred")),type="heatmap",colorbar=list(title="Scaled Value",len=0.4)) %>%
layout(yaxis=list(title=NULL),xaxis=list(tickangle=90,tickvals=10,ticktext="X-Label"))
Thanks
r ggplot2 axis-labels
Can you post desired result as your description is not too clear? This may involve using image software to sketch out for us: Paint, Paintbrush, Pinta, etc.
– Parfait
Nov 13 '18 at 18:24
add a comment |
I want to plot a heatmap
using ggplot2
, and I want to customize the x-axis ticks - location, text, and angle.
Here are my example data:
set.seed(1)
df <- reshape2::melt(matrix(rnorm(100*20),100,20,dimnames = list(paste0("G",1:100),paste0("S",1:20))))
I'd like to have the x-axis tick have only a single tick value, located in the middle of the axis range - 10, with the label: "X-Label", at a 90 degrees angle. In addition, I do not want an x-axis title.
Here's what I'm trying:
library(ggplot2)
ggplot(data=df,mapping=aes(x=Var2,y=Var1,fill=value))+
geom_tile()+theme_minimal()+scale_fill_gradient2(name="Scaled Value",low="darkblue",mid="gray",high="darkred")+
scale_x_discrete(breaks=10,labels="X-Label")+theme(axis.title.x=element_blank(),axis.text.x=element_text(angle=90,hjust=1,vjust=0.5))
So, obviously it's not working.
I thought that I can define the angle as part of the scale_x_discrete
arguments but couldn't find that option.
Also, note that I do not want an x-axis title but only the tick text. So I'm not sure why the breaks
and labels
arguments in scale_x_discrete
are ignored.
Here's the desired outcome using plotly
:
library(plotly)
library(dplyr)
plot_ly(z=c(df$value),x=df$Var2,y=df$Var1,colors=grDevices::colorRamp(c("darkblue","gray","darkred")),type="heatmap",colorbar=list(title="Scaled Value",len=0.4)) %>%
layout(yaxis=list(title=NULL),xaxis=list(tickangle=90,tickvals=10,ticktext="X-Label"))
Thanks
r ggplot2 axis-labels
I want to plot a heatmap
using ggplot2
, and I want to customize the x-axis ticks - location, text, and angle.
Here are my example data:
set.seed(1)
df <- reshape2::melt(matrix(rnorm(100*20),100,20,dimnames = list(paste0("G",1:100),paste0("S",1:20))))
I'd like to have the x-axis tick have only a single tick value, located in the middle of the axis range - 10, with the label: "X-Label", at a 90 degrees angle. In addition, I do not want an x-axis title.
Here's what I'm trying:
library(ggplot2)
ggplot(data=df,mapping=aes(x=Var2,y=Var1,fill=value))+
geom_tile()+theme_minimal()+scale_fill_gradient2(name="Scaled Value",low="darkblue",mid="gray",high="darkred")+
scale_x_discrete(breaks=10,labels="X-Label")+theme(axis.title.x=element_blank(),axis.text.x=element_text(angle=90,hjust=1,vjust=0.5))
So, obviously it's not working.
I thought that I can define the angle as part of the scale_x_discrete
arguments but couldn't find that option.
Also, note that I do not want an x-axis title but only the tick text. So I'm not sure why the breaks
and labels
arguments in scale_x_discrete
are ignored.
Here's the desired outcome using plotly
:
library(plotly)
library(dplyr)
plot_ly(z=c(df$value),x=df$Var2,y=df$Var1,colors=grDevices::colorRamp(c("darkblue","gray","darkred")),type="heatmap",colorbar=list(title="Scaled Value",len=0.4)) %>%
layout(yaxis=list(title=NULL),xaxis=list(tickangle=90,tickvals=10,ticktext="X-Label"))
Thanks
r ggplot2 axis-labels
r ggplot2 axis-labels
edited Nov 13 '18 at 18:32
dan
asked Nov 13 '18 at 18:05
dandan
1,45341045
1,45341045
Can you post desired result as your description is not too clear? This may involve using image software to sketch out for us: Paint, Paintbrush, Pinta, etc.
– Parfait
Nov 13 '18 at 18:24
add a comment |
Can you post desired result as your description is not too clear? This may involve using image software to sketch out for us: Paint, Paintbrush, Pinta, etc.
– Parfait
Nov 13 '18 at 18:24
Can you post desired result as your description is not too clear? This may involve using image software to sketch out for us: Paint, Paintbrush, Pinta, etc.
– Parfait
Nov 13 '18 at 18:24
Can you post desired result as your description is not too clear? This may involve using image software to sketch out for us: Paint, Paintbrush, Pinta, etc.
– Parfait
Nov 13 '18 at 18:24
add a comment |
2 Answers
2
active
oldest
votes
I think that specifying breaks = 10
is causing your axis labels to go unprinted. 10
isn't one of your values, and it doesn't fit the description of a valid argument in ?scale_x_discrete
, so let's use a real x-axis value instead. You say "right in the middle", so I'll use "G50"
since your x-axis values range from "G1"
to "G100"
:
ggplot(data = df,
mapping = aes(x = Var1, y = Var2, fill = value)) +
geom_tile() +
theme_minimal() +
scale_fill_gradient2(
name = "Scaled Value",
low = "darkblue",
mid = "gray",
high = "darkred"
) +
scale_x_discrete(breaks = "G50", labels = "X-Label", name = "") +
theme(
axis.text.x = element_text(
angle = 90,
hjust = 1,
vjust = 0.5
))
I also specified name = ""
in the scale
function instead of using theme
to disable the axis title.
If you need to dynamically calculate the middle level, it's a bit messy but this should work:
breaks = levels(df$Var1)[ceiling(length(levels(df$Var1)) / 2)]
add a comment |
The breaks option inside scale_x_discrete
does not specify the number of breaks but one of:
- NULL for no breaks
- waiver() for the default breaks computed by the transformation object
- A character vector of breaks
- A function that takes the limits as input and returns breaks as output
I find it easiest just to define the character vectors. In this case one half over at the 50th element.
set.seed(1)
df <- reshape2::melt(matrix(rnorm(100*20),100,20,dimnames = list(paste0("G",1:100),paste0("S",1:20))))
library(ggplot2)
ggplot(data=df,mapping=aes(x=Var1,y=Var2,fill=value))+
geom_tile() +
theme_minimal() +
scale_fill_gradient2(name="Scaled Value",low="darkblue",mid="gray",high="darkred") +
scale_x_discrete(breaks=unique(df$Var1)[50],labels="X-Label") +
theme(axis.title.x=element_blank(), axis.text.x=element_text(angle=90,hjust=1,vjust=0.5))
#Use this line instead to label along the axis
#scale_x_discrete(breaks=unique(df$Var1)[seq(1,100, 10)],labels=unique(df$Var1)[seq(1,100, 10)]) +
Hopefully this is what you are looking for.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287033%2fcustomizing-the-angle-in-a-ggplot-x-axis-tick-labels%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think that specifying breaks = 10
is causing your axis labels to go unprinted. 10
isn't one of your values, and it doesn't fit the description of a valid argument in ?scale_x_discrete
, so let's use a real x-axis value instead. You say "right in the middle", so I'll use "G50"
since your x-axis values range from "G1"
to "G100"
:
ggplot(data = df,
mapping = aes(x = Var1, y = Var2, fill = value)) +
geom_tile() +
theme_minimal() +
scale_fill_gradient2(
name = "Scaled Value",
low = "darkblue",
mid = "gray",
high = "darkred"
) +
scale_x_discrete(breaks = "G50", labels = "X-Label", name = "") +
theme(
axis.text.x = element_text(
angle = 90,
hjust = 1,
vjust = 0.5
))
I also specified name = ""
in the scale
function instead of using theme
to disable the axis title.
If you need to dynamically calculate the middle level, it's a bit messy but this should work:
breaks = levels(df$Var1)[ceiling(length(levels(df$Var1)) / 2)]
add a comment |
I think that specifying breaks = 10
is causing your axis labels to go unprinted. 10
isn't one of your values, and it doesn't fit the description of a valid argument in ?scale_x_discrete
, so let's use a real x-axis value instead. You say "right in the middle", so I'll use "G50"
since your x-axis values range from "G1"
to "G100"
:
ggplot(data = df,
mapping = aes(x = Var1, y = Var2, fill = value)) +
geom_tile() +
theme_minimal() +
scale_fill_gradient2(
name = "Scaled Value",
low = "darkblue",
mid = "gray",
high = "darkred"
) +
scale_x_discrete(breaks = "G50", labels = "X-Label", name = "") +
theme(
axis.text.x = element_text(
angle = 90,
hjust = 1,
vjust = 0.5
))
I also specified name = ""
in the scale
function instead of using theme
to disable the axis title.
If you need to dynamically calculate the middle level, it's a bit messy but this should work:
breaks = levels(df$Var1)[ceiling(length(levels(df$Var1)) / 2)]
add a comment |
I think that specifying breaks = 10
is causing your axis labels to go unprinted. 10
isn't one of your values, and it doesn't fit the description of a valid argument in ?scale_x_discrete
, so let's use a real x-axis value instead. You say "right in the middle", so I'll use "G50"
since your x-axis values range from "G1"
to "G100"
:
ggplot(data = df,
mapping = aes(x = Var1, y = Var2, fill = value)) +
geom_tile() +
theme_minimal() +
scale_fill_gradient2(
name = "Scaled Value",
low = "darkblue",
mid = "gray",
high = "darkred"
) +
scale_x_discrete(breaks = "G50", labels = "X-Label", name = "") +
theme(
axis.text.x = element_text(
angle = 90,
hjust = 1,
vjust = 0.5
))
I also specified name = ""
in the scale
function instead of using theme
to disable the axis title.
If you need to dynamically calculate the middle level, it's a bit messy but this should work:
breaks = levels(df$Var1)[ceiling(length(levels(df$Var1)) / 2)]
I think that specifying breaks = 10
is causing your axis labels to go unprinted. 10
isn't one of your values, and it doesn't fit the description of a valid argument in ?scale_x_discrete
, so let's use a real x-axis value instead. You say "right in the middle", so I'll use "G50"
since your x-axis values range from "G1"
to "G100"
:
ggplot(data = df,
mapping = aes(x = Var1, y = Var2, fill = value)) +
geom_tile() +
theme_minimal() +
scale_fill_gradient2(
name = "Scaled Value",
low = "darkblue",
mid = "gray",
high = "darkred"
) +
scale_x_discrete(breaks = "G50", labels = "X-Label", name = "") +
theme(
axis.text.x = element_text(
angle = 90,
hjust = 1,
vjust = 0.5
))
I also specified name = ""
in the scale
function instead of using theme
to disable the axis title.
If you need to dynamically calculate the middle level, it's a bit messy but this should work:
breaks = levels(df$Var1)[ceiling(length(levels(df$Var1)) / 2)]
edited Nov 13 '18 at 18:37
answered Nov 13 '18 at 18:31
GregorGregor
63.6k990170
63.6k990170
add a comment |
add a comment |
The breaks option inside scale_x_discrete
does not specify the number of breaks but one of:
- NULL for no breaks
- waiver() for the default breaks computed by the transformation object
- A character vector of breaks
- A function that takes the limits as input and returns breaks as output
I find it easiest just to define the character vectors. In this case one half over at the 50th element.
set.seed(1)
df <- reshape2::melt(matrix(rnorm(100*20),100,20,dimnames = list(paste0("G",1:100),paste0("S",1:20))))
library(ggplot2)
ggplot(data=df,mapping=aes(x=Var1,y=Var2,fill=value))+
geom_tile() +
theme_minimal() +
scale_fill_gradient2(name="Scaled Value",low="darkblue",mid="gray",high="darkred") +
scale_x_discrete(breaks=unique(df$Var1)[50],labels="X-Label") +
theme(axis.title.x=element_blank(), axis.text.x=element_text(angle=90,hjust=1,vjust=0.5))
#Use this line instead to label along the axis
#scale_x_discrete(breaks=unique(df$Var1)[seq(1,100, 10)],labels=unique(df$Var1)[seq(1,100, 10)]) +
Hopefully this is what you are looking for.
add a comment |
The breaks option inside scale_x_discrete
does not specify the number of breaks but one of:
- NULL for no breaks
- waiver() for the default breaks computed by the transformation object
- A character vector of breaks
- A function that takes the limits as input and returns breaks as output
I find it easiest just to define the character vectors. In this case one half over at the 50th element.
set.seed(1)
df <- reshape2::melt(matrix(rnorm(100*20),100,20,dimnames = list(paste0("G",1:100),paste0("S",1:20))))
library(ggplot2)
ggplot(data=df,mapping=aes(x=Var1,y=Var2,fill=value))+
geom_tile() +
theme_minimal() +
scale_fill_gradient2(name="Scaled Value",low="darkblue",mid="gray",high="darkred") +
scale_x_discrete(breaks=unique(df$Var1)[50],labels="X-Label") +
theme(axis.title.x=element_blank(), axis.text.x=element_text(angle=90,hjust=1,vjust=0.5))
#Use this line instead to label along the axis
#scale_x_discrete(breaks=unique(df$Var1)[seq(1,100, 10)],labels=unique(df$Var1)[seq(1,100, 10)]) +
Hopefully this is what you are looking for.
add a comment |
The breaks option inside scale_x_discrete
does not specify the number of breaks but one of:
- NULL for no breaks
- waiver() for the default breaks computed by the transformation object
- A character vector of breaks
- A function that takes the limits as input and returns breaks as output
I find it easiest just to define the character vectors. In this case one half over at the 50th element.
set.seed(1)
df <- reshape2::melt(matrix(rnorm(100*20),100,20,dimnames = list(paste0("G",1:100),paste0("S",1:20))))
library(ggplot2)
ggplot(data=df,mapping=aes(x=Var1,y=Var2,fill=value))+
geom_tile() +
theme_minimal() +
scale_fill_gradient2(name="Scaled Value",low="darkblue",mid="gray",high="darkred") +
scale_x_discrete(breaks=unique(df$Var1)[50],labels="X-Label") +
theme(axis.title.x=element_blank(), axis.text.x=element_text(angle=90,hjust=1,vjust=0.5))
#Use this line instead to label along the axis
#scale_x_discrete(breaks=unique(df$Var1)[seq(1,100, 10)],labels=unique(df$Var1)[seq(1,100, 10)]) +
Hopefully this is what you are looking for.
The breaks option inside scale_x_discrete
does not specify the number of breaks but one of:
- NULL for no breaks
- waiver() for the default breaks computed by the transformation object
- A character vector of breaks
- A function that takes the limits as input and returns breaks as output
I find it easiest just to define the character vectors. In this case one half over at the 50th element.
set.seed(1)
df <- reshape2::melt(matrix(rnorm(100*20),100,20,dimnames = list(paste0("G",1:100),paste0("S",1:20))))
library(ggplot2)
ggplot(data=df,mapping=aes(x=Var1,y=Var2,fill=value))+
geom_tile() +
theme_minimal() +
scale_fill_gradient2(name="Scaled Value",low="darkblue",mid="gray",high="darkred") +
scale_x_discrete(breaks=unique(df$Var1)[50],labels="X-Label") +
theme(axis.title.x=element_blank(), axis.text.x=element_text(angle=90,hjust=1,vjust=0.5))
#Use this line instead to label along the axis
#scale_x_discrete(breaks=unique(df$Var1)[seq(1,100, 10)],labels=unique(df$Var1)[seq(1,100, 10)]) +
Hopefully this is what you are looking for.
edited Nov 13 '18 at 18:40
answered Nov 13 '18 at 18:31
Dave2eDave2e
6,811112328
6,811112328
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53287033%2fcustomizing-the-angle-in-a-ggplot-x-axis-tick-labels%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Can you post desired result as your description is not too clear? This may involve using image software to sketch out for us: Paint, Paintbrush, Pinta, etc.
– Parfait
Nov 13 '18 at 18:24