R Overlay Line Plot on BarPlot with 2 axesggplot2 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
declaring a variable twice in IIFE
Why is this code 6.5x slower with optimizations enabled?
Download, install and reboot computer at night if needed
What typically incentivizes a professor to change jobs to a lower ranking university?
I probably found a bug with the sudo apt install function
Is there really no realistic way for a skeleton monster to move around without magic?
The magic money tree problem
The use of multiple foreign keys on same column in SQL Server
How to calculate implied correlation via observed market price (Margrabe option)
Can a German sentence have two subjects?
Extreme, but not acceptable situation and I can't start the work tomorrow morning
Schwarzchild Radius of the Universe
How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?
What do you call a Matrix-like slowdown and camera movement effect?
Why CLRS example on residual networks does not follows its formula?
Why has Russell's definition of numbers using equivalence classes been finally abandoned? ( If it has actually been abandoned).
How can bays and straits be determined in a procedurally generated map?
Can I make popcorn with any corn?
What is the meaning of "of trouble" in the following sentence?
Is it possible to do 50 km distance without any previous training?
How old can references or sources in a thesis be?
What is the command to reset a PC without deleting any files
How is it possible for user's password to be changed after storage was encrypted? (on OS X, Android)
Motorized valve interfering with button?
R Overlay Line Plot on BarPlot with 2 axes
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
4,8351337
4,8351337
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