How can I change axis label in ggplot?
I created a histogram in R. At the moment, the numbers 1 - 8 are written on the x-axis (for each number a bar). I would like to change the numbers into the wind direction, e.g. instead of 1, there should stand "west".
I tried:
scale_x_discrete(labels=c("1" = "North", "2" = "North East", "3" = "East", "4"= "South East", "5"= "South", "6"="South West", "7"="West", "8"="North West"))
But is not working.I also tried:
scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"), labels=c("North", "North East", "East", "South East", "South", "South West", "West", "North West"))
Here is my script:
options(stringsAsFactors = FALSE)
input1 <- "C:\Users\wind_direction.csv"
wind_direction <- read.csv(input1, sep=";")
library(ggplot2)
p3 <- ggplot(wind_direction, aes(x=winddirection)) +
geom_bar(color="black", fill="grey", width=0.9)+
theme_bw() +
scale_y_continuous(limits = c(0, 55), breaks = seq(0,55,10),expand=c(0,0)) +
scale_x_discrete(labels = c("1" = "North", "2" = "North East", "3" = "East", "4"= "South East", "5"= "South", "6"="South West", "7"="West", "8"="North West"))
print(p3)
Here is a sample of my data:
head(wind_direction)
day time winddirection
1 31.07.2018 12:51:57 3
2 31.07.2018 12:55:16 3
3 31.07.2018 12:56:29 3
4 31.07.2018 13:25:05 3
5 31.07.2018 13:36:54 3
6 31.07.2018 13:55:37 3
r ggplot2
add a comment |
I created a histogram in R. At the moment, the numbers 1 - 8 are written on the x-axis (for each number a bar). I would like to change the numbers into the wind direction, e.g. instead of 1, there should stand "west".
I tried:
scale_x_discrete(labels=c("1" = "North", "2" = "North East", "3" = "East", "4"= "South East", "5"= "South", "6"="South West", "7"="West", "8"="North West"))
But is not working.I also tried:
scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"), labels=c("North", "North East", "East", "South East", "South", "South West", "West", "North West"))
Here is my script:
options(stringsAsFactors = FALSE)
input1 <- "C:\Users\wind_direction.csv"
wind_direction <- read.csv(input1, sep=";")
library(ggplot2)
p3 <- ggplot(wind_direction, aes(x=winddirection)) +
geom_bar(color="black", fill="grey", width=0.9)+
theme_bw() +
scale_y_continuous(limits = c(0, 55), breaks = seq(0,55,10),expand=c(0,0)) +
scale_x_discrete(labels = c("1" = "North", "2" = "North East", "3" = "East", "4"= "South East", "5"= "South", "6"="South West", "7"="West", "8"="North West"))
print(p3)
Here is a sample of my data:
head(wind_direction)
day time winddirection
1 31.07.2018 12:51:57 3
2 31.07.2018 12:55:16 3
3 31.07.2018 12:56:29 3
4 31.07.2018 13:25:05 3
5 31.07.2018 13:36:54 3
6 31.07.2018 13:55:37 3
r ggplot2
Can you post sample data indput
format? Please edit the question with the output ofdput(wind_direction)
. Or, if it is too big with the output ofdput(head(wind_direction, 20))
.
– Rui Barradas
Dec 27 at 12:58
Have you tried to replaceaes(x=winddirection)
withaes(x=as.character(winddirection))
?
– Valentin
Dec 27 at 13:05
See if this question can help.
– Rui Barradas
Dec 27 at 13:09
add a comment |
I created a histogram in R. At the moment, the numbers 1 - 8 are written on the x-axis (for each number a bar). I would like to change the numbers into the wind direction, e.g. instead of 1, there should stand "west".
I tried:
scale_x_discrete(labels=c("1" = "North", "2" = "North East", "3" = "East", "4"= "South East", "5"= "South", "6"="South West", "7"="West", "8"="North West"))
But is not working.I also tried:
scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"), labels=c("North", "North East", "East", "South East", "South", "South West", "West", "North West"))
Here is my script:
options(stringsAsFactors = FALSE)
input1 <- "C:\Users\wind_direction.csv"
wind_direction <- read.csv(input1, sep=";")
library(ggplot2)
p3 <- ggplot(wind_direction, aes(x=winddirection)) +
geom_bar(color="black", fill="grey", width=0.9)+
theme_bw() +
scale_y_continuous(limits = c(0, 55), breaks = seq(0,55,10),expand=c(0,0)) +
scale_x_discrete(labels = c("1" = "North", "2" = "North East", "3" = "East", "4"= "South East", "5"= "South", "6"="South West", "7"="West", "8"="North West"))
print(p3)
Here is a sample of my data:
head(wind_direction)
day time winddirection
1 31.07.2018 12:51:57 3
2 31.07.2018 12:55:16 3
3 31.07.2018 12:56:29 3
4 31.07.2018 13:25:05 3
5 31.07.2018 13:36:54 3
6 31.07.2018 13:55:37 3
r ggplot2
I created a histogram in R. At the moment, the numbers 1 - 8 are written on the x-axis (for each number a bar). I would like to change the numbers into the wind direction, e.g. instead of 1, there should stand "west".
I tried:
scale_x_discrete(labels=c("1" = "North", "2" = "North East", "3" = "East", "4"= "South East", "5"= "South", "6"="South West", "7"="West", "8"="North West"))
But is not working.I also tried:
scale_x_discrete(breaks=c("1","2","3", "4", "5", "6", "7", "8"), labels=c("North", "North East", "East", "South East", "South", "South West", "West", "North West"))
Here is my script:
options(stringsAsFactors = FALSE)
input1 <- "C:\Users\wind_direction.csv"
wind_direction <- read.csv(input1, sep=";")
library(ggplot2)
p3 <- ggplot(wind_direction, aes(x=winddirection)) +
geom_bar(color="black", fill="grey", width=0.9)+
theme_bw() +
scale_y_continuous(limits = c(0, 55), breaks = seq(0,55,10),expand=c(0,0)) +
scale_x_discrete(labels = c("1" = "North", "2" = "North East", "3" = "East", "4"= "South East", "5"= "South", "6"="South West", "7"="West", "8"="North West"))
print(p3)
Here is a sample of my data:
head(wind_direction)
day time winddirection
1 31.07.2018 12:51:57 3
2 31.07.2018 12:55:16 3
3 31.07.2018 12:56:29 3
4 31.07.2018 13:25:05 3
5 31.07.2018 13:36:54 3
6 31.07.2018 13:55:37 3
r ggplot2
r ggplot2
edited Dec 27 at 12:57
Rui Barradas
16k41730
16k41730
asked Dec 27 at 12:49
Dolphin94
124
124
Can you post sample data indput
format? Please edit the question with the output ofdput(wind_direction)
. Or, if it is too big with the output ofdput(head(wind_direction, 20))
.
– Rui Barradas
Dec 27 at 12:58
Have you tried to replaceaes(x=winddirection)
withaes(x=as.character(winddirection))
?
– Valentin
Dec 27 at 13:05
See if this question can help.
– Rui Barradas
Dec 27 at 13:09
add a comment |
Can you post sample data indput
format? Please edit the question with the output ofdput(wind_direction)
. Or, if it is too big with the output ofdput(head(wind_direction, 20))
.
– Rui Barradas
Dec 27 at 12:58
Have you tried to replaceaes(x=winddirection)
withaes(x=as.character(winddirection))
?
– Valentin
Dec 27 at 13:05
See if this question can help.
– Rui Barradas
Dec 27 at 13:09
Can you post sample data in
dput
format? Please edit the question with the output of dput(wind_direction)
. Or, if it is too big with the output of dput(head(wind_direction, 20))
.– Rui Barradas
Dec 27 at 12:58
Can you post sample data in
dput
format? Please edit the question with the output of dput(wind_direction)
. Or, if it is too big with the output of dput(head(wind_direction, 20))
.– Rui Barradas
Dec 27 at 12:58
Have you tried to replace
aes(x=winddirection)
with aes(x=as.character(winddirection))
?– Valentin
Dec 27 at 13:05
Have you tried to replace
aes(x=winddirection)
with aes(x=as.character(winddirection))
?– Valentin
Dec 27 at 13:05
See if this question can help.
– Rui Barradas
Dec 27 at 13:09
See if this question can help.
– Rui Barradas
Dec 27 at 13:09
add a comment |
1 Answer
1
active
oldest
votes
I think you did a good job with your code and a fast solution involves just to replace aes(x=winddirection)
with aes(x=as.character(winddirection))
or aes(x=as.factor(winddirection))
.
So, you just need to be sure that winddirection
is character or factor when you map it into x
.
Just be sure you have the right labeling. You mention in your question that 1 should be west, but in scale_x_discrete
you declare that 1 is north.
Thanks a lot for all the answers. @Valentin: I replaced aes(x=winddirection) with aes(x=as.character(winddirection)) or aes(x=as.factor(winddirection)). But unfortunately, it gives me the error: "Aesthetics must be either length 1 or the same as the data (224): x".
– Dolphin94
Dec 27 at 13:23
Hi @Dolphin94. It works for me. Try to give it a clean run (clean your environment and run again). It sounds like maybe you edited something in your data.framewind_direction
(?) I can replicate your error if instead ofwinddirection
I usewind_direction
inaes(x=as.character(winddirection))
- might that be the case?
– Valentin
Dec 27 at 13:29
Thank you so much. Now it is working. I used aes(x=as.character(winddirection)), but the correct form is aes(x=as.character(wind_direction))
– Dolphin94
Dec 27 at 13:40
That is confusing because you cannot map your data framewind_direction
intox
, but your columnwinddirection
. Did you swap their names somehow?
– Valentin
Dec 27 at 13:44
I am sorry for confusing you. Yes, I swapped inadvertently their names in the script. The name of my excel file is wind_direction and the name of the column is winddirection. Therefore, I wrote: aes(x=as.character(winddirection). And now it is working.
– Dolphin94
Dec 27 at 14:14
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%2f53945383%2fhow-can-i-change-axis-label-in-ggplot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I think you did a good job with your code and a fast solution involves just to replace aes(x=winddirection)
with aes(x=as.character(winddirection))
or aes(x=as.factor(winddirection))
.
So, you just need to be sure that winddirection
is character or factor when you map it into x
.
Just be sure you have the right labeling. You mention in your question that 1 should be west, but in scale_x_discrete
you declare that 1 is north.
Thanks a lot for all the answers. @Valentin: I replaced aes(x=winddirection) with aes(x=as.character(winddirection)) or aes(x=as.factor(winddirection)). But unfortunately, it gives me the error: "Aesthetics must be either length 1 or the same as the data (224): x".
– Dolphin94
Dec 27 at 13:23
Hi @Dolphin94. It works for me. Try to give it a clean run (clean your environment and run again). It sounds like maybe you edited something in your data.framewind_direction
(?) I can replicate your error if instead ofwinddirection
I usewind_direction
inaes(x=as.character(winddirection))
- might that be the case?
– Valentin
Dec 27 at 13:29
Thank you so much. Now it is working. I used aes(x=as.character(winddirection)), but the correct form is aes(x=as.character(wind_direction))
– Dolphin94
Dec 27 at 13:40
That is confusing because you cannot map your data framewind_direction
intox
, but your columnwinddirection
. Did you swap their names somehow?
– Valentin
Dec 27 at 13:44
I am sorry for confusing you. Yes, I swapped inadvertently their names in the script. The name of my excel file is wind_direction and the name of the column is winddirection. Therefore, I wrote: aes(x=as.character(winddirection). And now it is working.
– Dolphin94
Dec 27 at 14:14
add a comment |
I think you did a good job with your code and a fast solution involves just to replace aes(x=winddirection)
with aes(x=as.character(winddirection))
or aes(x=as.factor(winddirection))
.
So, you just need to be sure that winddirection
is character or factor when you map it into x
.
Just be sure you have the right labeling. You mention in your question that 1 should be west, but in scale_x_discrete
you declare that 1 is north.
Thanks a lot for all the answers. @Valentin: I replaced aes(x=winddirection) with aes(x=as.character(winddirection)) or aes(x=as.factor(winddirection)). But unfortunately, it gives me the error: "Aesthetics must be either length 1 or the same as the data (224): x".
– Dolphin94
Dec 27 at 13:23
Hi @Dolphin94. It works for me. Try to give it a clean run (clean your environment and run again). It sounds like maybe you edited something in your data.framewind_direction
(?) I can replicate your error if instead ofwinddirection
I usewind_direction
inaes(x=as.character(winddirection))
- might that be the case?
– Valentin
Dec 27 at 13:29
Thank you so much. Now it is working. I used aes(x=as.character(winddirection)), but the correct form is aes(x=as.character(wind_direction))
– Dolphin94
Dec 27 at 13:40
That is confusing because you cannot map your data framewind_direction
intox
, but your columnwinddirection
. Did you swap their names somehow?
– Valentin
Dec 27 at 13:44
I am sorry for confusing you. Yes, I swapped inadvertently their names in the script. The name of my excel file is wind_direction and the name of the column is winddirection. Therefore, I wrote: aes(x=as.character(winddirection). And now it is working.
– Dolphin94
Dec 27 at 14:14
add a comment |
I think you did a good job with your code and a fast solution involves just to replace aes(x=winddirection)
with aes(x=as.character(winddirection))
or aes(x=as.factor(winddirection))
.
So, you just need to be sure that winddirection
is character or factor when you map it into x
.
Just be sure you have the right labeling. You mention in your question that 1 should be west, but in scale_x_discrete
you declare that 1 is north.
I think you did a good job with your code and a fast solution involves just to replace aes(x=winddirection)
with aes(x=as.character(winddirection))
or aes(x=as.factor(winddirection))
.
So, you just need to be sure that winddirection
is character or factor when you map it into x
.
Just be sure you have the right labeling. You mention in your question that 1 should be west, but in scale_x_discrete
you declare that 1 is north.
answered Dec 27 at 13:12
Valentin
1,7021028
1,7021028
Thanks a lot for all the answers. @Valentin: I replaced aes(x=winddirection) with aes(x=as.character(winddirection)) or aes(x=as.factor(winddirection)). But unfortunately, it gives me the error: "Aesthetics must be either length 1 or the same as the data (224): x".
– Dolphin94
Dec 27 at 13:23
Hi @Dolphin94. It works for me. Try to give it a clean run (clean your environment and run again). It sounds like maybe you edited something in your data.framewind_direction
(?) I can replicate your error if instead ofwinddirection
I usewind_direction
inaes(x=as.character(winddirection))
- might that be the case?
– Valentin
Dec 27 at 13:29
Thank you so much. Now it is working. I used aes(x=as.character(winddirection)), but the correct form is aes(x=as.character(wind_direction))
– Dolphin94
Dec 27 at 13:40
That is confusing because you cannot map your data framewind_direction
intox
, but your columnwinddirection
. Did you swap their names somehow?
– Valentin
Dec 27 at 13:44
I am sorry for confusing you. Yes, I swapped inadvertently their names in the script. The name of my excel file is wind_direction and the name of the column is winddirection. Therefore, I wrote: aes(x=as.character(winddirection). And now it is working.
– Dolphin94
Dec 27 at 14:14
add a comment |
Thanks a lot for all the answers. @Valentin: I replaced aes(x=winddirection) with aes(x=as.character(winddirection)) or aes(x=as.factor(winddirection)). But unfortunately, it gives me the error: "Aesthetics must be either length 1 or the same as the data (224): x".
– Dolphin94
Dec 27 at 13:23
Hi @Dolphin94. It works for me. Try to give it a clean run (clean your environment and run again). It sounds like maybe you edited something in your data.framewind_direction
(?) I can replicate your error if instead ofwinddirection
I usewind_direction
inaes(x=as.character(winddirection))
- might that be the case?
– Valentin
Dec 27 at 13:29
Thank you so much. Now it is working. I used aes(x=as.character(winddirection)), but the correct form is aes(x=as.character(wind_direction))
– Dolphin94
Dec 27 at 13:40
That is confusing because you cannot map your data framewind_direction
intox
, but your columnwinddirection
. Did you swap their names somehow?
– Valentin
Dec 27 at 13:44
I am sorry for confusing you. Yes, I swapped inadvertently their names in the script. The name of my excel file is wind_direction and the name of the column is winddirection. Therefore, I wrote: aes(x=as.character(winddirection). And now it is working.
– Dolphin94
Dec 27 at 14:14
Thanks a lot for all the answers. @Valentin: I replaced aes(x=winddirection) with aes(x=as.character(winddirection)) or aes(x=as.factor(winddirection)). But unfortunately, it gives me the error: "Aesthetics must be either length 1 or the same as the data (224): x".
– Dolphin94
Dec 27 at 13:23
Thanks a lot for all the answers. @Valentin: I replaced aes(x=winddirection) with aes(x=as.character(winddirection)) or aes(x=as.factor(winddirection)). But unfortunately, it gives me the error: "Aesthetics must be either length 1 or the same as the data (224): x".
– Dolphin94
Dec 27 at 13:23
Hi @Dolphin94. It works for me. Try to give it a clean run (clean your environment and run again). It sounds like maybe you edited something in your data.frame
wind_direction
(?) I can replicate your error if instead of winddirection
I use wind_direction
in aes(x=as.character(winddirection))
- might that be the case?– Valentin
Dec 27 at 13:29
Hi @Dolphin94. It works for me. Try to give it a clean run (clean your environment and run again). It sounds like maybe you edited something in your data.frame
wind_direction
(?) I can replicate your error if instead of winddirection
I use wind_direction
in aes(x=as.character(winddirection))
- might that be the case?– Valentin
Dec 27 at 13:29
Thank you so much. Now it is working. I used aes(x=as.character(winddirection)), but the correct form is aes(x=as.character(wind_direction))
– Dolphin94
Dec 27 at 13:40
Thank you so much. Now it is working. I used aes(x=as.character(winddirection)), but the correct form is aes(x=as.character(wind_direction))
– Dolphin94
Dec 27 at 13:40
That is confusing because you cannot map your data frame
wind_direction
into x
, but your column winddirection
. Did you swap their names somehow?– Valentin
Dec 27 at 13:44
That is confusing because you cannot map your data frame
wind_direction
into x
, but your column winddirection
. Did you swap their names somehow?– Valentin
Dec 27 at 13:44
I am sorry for confusing you. Yes, I swapped inadvertently their names in the script. The name of my excel file is wind_direction and the name of the column is winddirection. Therefore, I wrote: aes(x=as.character(winddirection). And now it is working.
– Dolphin94
Dec 27 at 14:14
I am sorry for confusing you. Yes, I swapped inadvertently their names in the script. The name of my excel file is wind_direction and the name of the column is winddirection. Therefore, I wrote: aes(x=as.character(winddirection). And now it is working.
– Dolphin94
Dec 27 at 14:14
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53945383%2fhow-can-i-change-axis-label-in-ggplot%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 sample data in
dput
format? Please edit the question with the output ofdput(wind_direction)
. Or, if it is too big with the output ofdput(head(wind_direction, 20))
.– Rui Barradas
Dec 27 at 12:58
Have you tried to replace
aes(x=winddirection)
withaes(x=as.character(winddirection))
?– Valentin
Dec 27 at 13:05
See if this question can help.
– Rui Barradas
Dec 27 at 13:09