R Overlay Line Plot on BarPlot with 2 axes Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!ggplot2 overlay of barplot and line plotPlot two graphs in same plot in RHow create a overlay plot with secondary y-axis?ggplot: line plot for discrete x-axisHow to superimpose a line graph on a barplot in ggplot2stacked barplot converting a variable into a presence absence based percentage for unrelated variables in ggplot2 ROverlaying barplot with line graphs using ggplot2Overlay histogram with kernel density for different data sets on different x-scaleError- State_count() must not be used ,. How to solve this?Removing axis labelling for one geom when multiple geoms are presentError: Don't know how to add e2 to a plot
In search of the origins of term censor, I hit a dead end stuck with the greek term, to censor, λογοκρίνω
TV series episode where humans nuke aliens before decrypting their message that states they come in peace
Married in secret, can marital status in passport be changed at a later date?
How would you suggest I follow up with coworkers about our deadline that's today?
Is it appropriate to mention a relatable company blog post when you're asked about the company?
What helicopter has the most rotor blades?
Does using the Inspiration rules for character defects encourage My Guy Syndrome?
When does Bran Stark remember Jamie pushing him?
Is Bran literally the world's memory?
Why do people think Winterfell crypts is the safest place for women, children & old people?
Preserving file and folder permissions with rsync
Is there an efficient way for synchronising audio events real-time with LEDs using an MCU?
Suing a Police Officer Instead of the Police Department
Philosophers who were composers?
What is the numbering system used for the DSN dishes?
What do you call an IPA symbol that lacks a name (e.g. ɲ)?
Coin Game with infinite paradox
`FindRoot [ ]`::jsing: Encountered a singular Jacobian at a point...WHY
How can I wire a 9-position switch so that each position turns on one more LED than the one before?
Are there existing rules/lore for MTG planeswalkers?
Processing ADC conversion result: DMA vs Processor Registers
false 'Security alert' from Google - every login generates mails from 'no-reply@accounts.google.com'
Is there a way to fake a method response using Mock or Stubs?
Determinant of a matrix with 2 equal rows
R Overlay Line Plot on BarPlot with 2 axes
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30 pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!ggplot2 overlay of barplot and line plotPlot two graphs in same plot in RHow create a overlay plot with secondary y-axis?ggplot: line plot for discrete x-axisHow to superimpose a line graph on a barplot in ggplot2stacked barplot converting a variable into a presence absence based percentage for unrelated variables in ggplot2 ROverlaying barplot with line graphs using ggplot2Overlay histogram with kernel density for different data sets on different x-scaleError- State_count() must not be used ,. How to solve this?Removing axis labelling for one geom when multiple geoms are presentError: Don't know how to add e2 to a plot
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have the following code and it is only producing a barplot without the line. How can I tweak this code to get the secondary line?
library(ggplot2)
p1 <- ggplot()
p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue")
p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
print(p1)
I've looked at ggplot2 overlay of barplot and line plot and I still can't figure out why the plot isn't showing the line.
r
add a comment |
I have the following code and it is only producing a barplot without the line. How can I tweak this code to get the secondary line?
library(ggplot2)
p1 <- ggplot()
p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue")
p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
print(p1)
I've looked at ggplot2 overlay of barplot and line plot and I still can't figure out why the plot isn't showing the line.
r
add a comment |
I have the following code and it is only producing a barplot without the line. How can I tweak this code to get the secondary line?
library(ggplot2)
p1 <- ggplot()
p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue")
p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
print(p1)
I've looked at ggplot2 overlay of barplot and line plot and I still can't figure out why the plot isn't showing the line.
r
I have the following code and it is only producing a barplot without the line. How can I tweak this code to get the secondary line?
library(ggplot2)
p1 <- ggplot()
p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue")
p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
print(p1)
I've looked at ggplot2 overlay of barplot and line plot and I still can't figure out why the plot isn't showing the line.
library(ggplot2)
p1 <- ggplot()
p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue")
p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
print(p1)
library(ggplot2)
p1 <- ggplot()
p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue")
p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
print(p1)
r
r
edited Mar 9 at 4:43
bross
asked Mar 8 at 6:02
brossbross
385
385
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You should provide a minimal data set to reproduce your problem, so I cannot test my solution, but I assume it is because a line on a factor scale does not work, so you should use:
p1 <- p1 + geom_line(data = subset(df, Year == 2006),
aes(x = State, y = Total.Poverty / 1000), colour = "blue")
instead.
I tried that and no success. Let me show you what I have. Reattaching file in original thread. df<-read.csv('Shiny/Vax.csv') df[4:17] <- lapply(df[4:17], as.numeric) colnames(df)[colnames(df)=="variable"] <- "Year" df <- df[,-1]
– bross
Mar 9 at 4:40
add a comment |
Had to add group = 1 in the code
library(ggplot2)
p1 <- ggplot()
p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue", group = 1)
p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
print(p1)
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%2f55057566%2fr-overlay-line-plot-on-barplot-with-2-axes%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
You should provide a minimal data set to reproduce your problem, so I cannot test my solution, but I assume it is because a line on a factor scale does not work, so you should use:
p1 <- p1 + geom_line(data = subset(df, Year == 2006),
aes(x = State, y = Total.Poverty / 1000), colour = "blue")
instead.
I tried that and no success. Let me show you what I have. Reattaching file in original thread. df<-read.csv('Shiny/Vax.csv') df[4:17] <- lapply(df[4:17], as.numeric) colnames(df)[colnames(df)=="variable"] <- "Year" df <- df[,-1]
– bross
Mar 9 at 4:40
add a comment |
You should provide a minimal data set to reproduce your problem, so I cannot test my solution, but I assume it is because a line on a factor scale does not work, so you should use:
p1 <- p1 + geom_line(data = subset(df, Year == 2006),
aes(x = State, y = Total.Poverty / 1000), colour = "blue")
instead.
I tried that and no success. Let me show you what I have. Reattaching file in original thread. df<-read.csv('Shiny/Vax.csv') df[4:17] <- lapply(df[4:17], as.numeric) colnames(df)[colnames(df)=="variable"] <- "Year" df <- df[,-1]
– bross
Mar 9 at 4:40
add a comment |
You should provide a minimal data set to reproduce your problem, so I cannot test my solution, but I assume it is because a line on a factor scale does not work, so you should use:
p1 <- p1 + geom_line(data = subset(df, Year == 2006),
aes(x = State, y = Total.Poverty / 1000), colour = "blue")
instead.
You should provide a minimal data set to reproduce your problem, so I cannot test my solution, but I assume it is because a line on a factor scale does not work, so you should use:
p1 <- p1 + geom_line(data = subset(df, Year == 2006),
aes(x = State, y = Total.Poverty / 1000), colour = "blue")
instead.
answered Mar 8 at 12:48
thothalthothal
5,5491438
5,5491438
I tried that and no success. Let me show you what I have. Reattaching file in original thread. df<-read.csv('Shiny/Vax.csv') df[4:17] <- lapply(df[4:17], as.numeric) colnames(df)[colnames(df)=="variable"] <- "Year" df <- df[,-1]
– bross
Mar 9 at 4:40
add a comment |
I tried that and no success. Let me show you what I have. Reattaching file in original thread. df<-read.csv('Shiny/Vax.csv') df[4:17] <- lapply(df[4:17], as.numeric) colnames(df)[colnames(df)=="variable"] <- "Year" df <- df[,-1]
– bross
Mar 9 at 4:40
I tried that and no success. Let me show you what I have. Reattaching file in original thread. df<-read.csv('Shiny/Vax.csv') df[4:17] <- lapply(df[4:17], as.numeric) colnames(df)[colnames(df)=="variable"] <- "Year" df <- df[,-1]
– bross
Mar 9 at 4:40
I tried that and no success. Let me show you what I have. Reattaching file in original thread. df<-read.csv('Shiny/Vax.csv') df[4:17] <- lapply(df[4:17], as.numeric) colnames(df)[colnames(df)=="variable"] <- "Year" df <- df[,-1]
– bross
Mar 9 at 4:40
add a comment |
Had to add group = 1 in the code
library(ggplot2)
p1 <- ggplot()
p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue", group = 1)
p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
print(p1)
add a comment |
Had to add group = 1 in the code
library(ggplot2)
p1 <- ggplot()
p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue", group = 1)
p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
print(p1)
add a comment |
Had to add group = 1 in the code
library(ggplot2)
p1 <- ggplot()
p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue", group = 1)
p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
print(p1)
Had to add group = 1 in the code
library(ggplot2)
p1 <- ggplot()
p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue", group = 1)
p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
print(p1)
library(ggplot2)
p1 <- ggplot()
p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue", group = 1)
p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
print(p1)
library(ggplot2)
p1 <- ggplot()
p1 <- p1 + geom_bar(data=subset(df, Year==2006), aes(x=factor(State),y=Rate), stat = "identity")
p1 <- p1 + xlab("State") + ylab("Rate") + theme(axis.text.x = element_text(angle = 60, hjust = 1))
p1 <- p1 + geom_line(data = subset(df, Year==2006), aes(x=factor(State),y=Total.Poverty/1000),colour = "blue", group = 1)
p1 <- p1 + scale_y_continuous(sec.axis = sec_axis(~.*1000, name = "Total Poverty"))
print(p1)
answered Mar 9 at 4:50
brossbross
385
385
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%2f55057566%2fr-overlay-line-plot-on-barplot-with-2-axes%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