ggplot lineplot for multiple rows of data - ggplot equivalent for matplot
I am trying to create a plot with a line for each sample which has 24 measured values (i.e. data to be plotted are in rows rather than columns). An example of my data looks like this:
structure(c("23.96000", "25.92000", "20.13000", "20.39000", "13.88000",
"14.97000", "11.56000", "12.75000", " 8.86000", "10.33000", " 8.96000",
" 9.87000", " 7.540000", " 8.160000", " 6.670000", " 7.430000",
" 7.060000", " 7.040000", " 6.250000", " 7.200000", " 6.400000",
" 6.380000", " 6.70000", " 6.05000", " 5.590000", " 6.310000",
" 6.000000", " 5.770000"), .Dim = c(2L, 14L), .Dimnames = list(
NULL, c("La", "Ce", "Pr", "Nd", "Sm", "Eu", "Gd", "Tb", "Dy",
"Ho", "Er", "Tm", "Yb", "Lu")))
I have succeeded in creating the plot I want in matplot with the following code:
m <- as.matrix(data)
REE <- c('La','Ce','Pr','Nd','Sm','Eu','Gd','Tb','Dy','Ho','Er','Tm','Yb','Lu')
m2 <- m[,11:24]
#Plotting with matplot
matplot(t(m2), type = "l", log="y", xaxt ="n",ylab="C/C_Chondrite",ylim=c(1,100))
axis(1, at=1:length(REE), labels=REE)
Which generates:
[REE plot][1]
I have tried the method described in this example: ggplot equivalent for matplot with only using geom_point() just to test out the function,
however I am currently getting a plot like this:
[bad plot][1]
Is anyone able to help?
r matplotlib ggplot2
add a comment |
I am trying to create a plot with a line for each sample which has 24 measured values (i.e. data to be plotted are in rows rather than columns). An example of my data looks like this:
structure(c("23.96000", "25.92000", "20.13000", "20.39000", "13.88000",
"14.97000", "11.56000", "12.75000", " 8.86000", "10.33000", " 8.96000",
" 9.87000", " 7.540000", " 8.160000", " 6.670000", " 7.430000",
" 7.060000", " 7.040000", " 6.250000", " 7.200000", " 6.400000",
" 6.380000", " 6.70000", " 6.05000", " 5.590000", " 6.310000",
" 6.000000", " 5.770000"), .Dim = c(2L, 14L), .Dimnames = list(
NULL, c("La", "Ce", "Pr", "Nd", "Sm", "Eu", "Gd", "Tb", "Dy",
"Ho", "Er", "Tm", "Yb", "Lu")))
I have succeeded in creating the plot I want in matplot with the following code:
m <- as.matrix(data)
REE <- c('La','Ce','Pr','Nd','Sm','Eu','Gd','Tb','Dy','Ho','Er','Tm','Yb','Lu')
m2 <- m[,11:24]
#Plotting with matplot
matplot(t(m2), type = "l", log="y", xaxt ="n",ylab="C/C_Chondrite",ylim=c(1,100))
axis(1, at=1:length(REE), labels=REE)
Which generates:
[REE plot][1]
I have tried the method described in this example: ggplot equivalent for matplot with only using geom_point() just to test out the function,
however I am currently getting a plot like this:
[bad plot][1]
Is anyone able to help?
r matplotlib ggplot2
Hi & welcome to Stack Overflow! Please provide yourdata
withdput(data)
to make a MCVE. Thanks!
– jay.sf
Nov 21 '18 at 18:20
ggplot
is powerful when you feed it long data, but unwieldy with wide data. I suggest you add a step before ggplot where you convert it, e.g.tidyr::gather(element, value, La:Lu)
...
– Jon Spring
Nov 21 '18 at 19:16
1
Hi @jay.sf I have changed this - thanks for the tip!
– lmm
Nov 21 '18 at 21:11
@jay.sf I just saw it - thank you!
– lmm
Nov 22 '18 at 14:33
add a comment |
I am trying to create a plot with a line for each sample which has 24 measured values (i.e. data to be plotted are in rows rather than columns). An example of my data looks like this:
structure(c("23.96000", "25.92000", "20.13000", "20.39000", "13.88000",
"14.97000", "11.56000", "12.75000", " 8.86000", "10.33000", " 8.96000",
" 9.87000", " 7.540000", " 8.160000", " 6.670000", " 7.430000",
" 7.060000", " 7.040000", " 6.250000", " 7.200000", " 6.400000",
" 6.380000", " 6.70000", " 6.05000", " 5.590000", " 6.310000",
" 6.000000", " 5.770000"), .Dim = c(2L, 14L), .Dimnames = list(
NULL, c("La", "Ce", "Pr", "Nd", "Sm", "Eu", "Gd", "Tb", "Dy",
"Ho", "Er", "Tm", "Yb", "Lu")))
I have succeeded in creating the plot I want in matplot with the following code:
m <- as.matrix(data)
REE <- c('La','Ce','Pr','Nd','Sm','Eu','Gd','Tb','Dy','Ho','Er','Tm','Yb','Lu')
m2 <- m[,11:24]
#Plotting with matplot
matplot(t(m2), type = "l", log="y", xaxt ="n",ylab="C/C_Chondrite",ylim=c(1,100))
axis(1, at=1:length(REE), labels=REE)
Which generates:
[REE plot][1]
I have tried the method described in this example: ggplot equivalent for matplot with only using geom_point() just to test out the function,
however I am currently getting a plot like this:
[bad plot][1]
Is anyone able to help?
r matplotlib ggplot2
I am trying to create a plot with a line for each sample which has 24 measured values (i.e. data to be plotted are in rows rather than columns). An example of my data looks like this:
structure(c("23.96000", "25.92000", "20.13000", "20.39000", "13.88000",
"14.97000", "11.56000", "12.75000", " 8.86000", "10.33000", " 8.96000",
" 9.87000", " 7.540000", " 8.160000", " 6.670000", " 7.430000",
" 7.060000", " 7.040000", " 6.250000", " 7.200000", " 6.400000",
" 6.380000", " 6.70000", " 6.05000", " 5.590000", " 6.310000",
" 6.000000", " 5.770000"), .Dim = c(2L, 14L), .Dimnames = list(
NULL, c("La", "Ce", "Pr", "Nd", "Sm", "Eu", "Gd", "Tb", "Dy",
"Ho", "Er", "Tm", "Yb", "Lu")))
I have succeeded in creating the plot I want in matplot with the following code:
m <- as.matrix(data)
REE <- c('La','Ce','Pr','Nd','Sm','Eu','Gd','Tb','Dy','Ho','Er','Tm','Yb','Lu')
m2 <- m[,11:24]
#Plotting with matplot
matplot(t(m2), type = "l", log="y", xaxt ="n",ylab="C/C_Chondrite",ylim=c(1,100))
axis(1, at=1:length(REE), labels=REE)
Which generates:
[REE plot][1]
I have tried the method described in this example: ggplot equivalent for matplot with only using geom_point() just to test out the function,
however I am currently getting a plot like this:
[bad plot][1]
Is anyone able to help?
r matplotlib ggplot2
r matplotlib ggplot2
edited Nov 21 '18 at 21:26
jay.sf
5,26221639
5,26221639
asked Nov 21 '18 at 18:18
lmmlmm
84
84
Hi & welcome to Stack Overflow! Please provide yourdata
withdput(data)
to make a MCVE. Thanks!
– jay.sf
Nov 21 '18 at 18:20
ggplot
is powerful when you feed it long data, but unwieldy with wide data. I suggest you add a step before ggplot where you convert it, e.g.tidyr::gather(element, value, La:Lu)
...
– Jon Spring
Nov 21 '18 at 19:16
1
Hi @jay.sf I have changed this - thanks for the tip!
– lmm
Nov 21 '18 at 21:11
@jay.sf I just saw it - thank you!
– lmm
Nov 22 '18 at 14:33
add a comment |
Hi & welcome to Stack Overflow! Please provide yourdata
withdput(data)
to make a MCVE. Thanks!
– jay.sf
Nov 21 '18 at 18:20
ggplot
is powerful when you feed it long data, but unwieldy with wide data. I suggest you add a step before ggplot where you convert it, e.g.tidyr::gather(element, value, La:Lu)
...
– Jon Spring
Nov 21 '18 at 19:16
1
Hi @jay.sf I have changed this - thanks for the tip!
– lmm
Nov 21 '18 at 21:11
@jay.sf I just saw it - thank you!
– lmm
Nov 22 '18 at 14:33
Hi & welcome to Stack Overflow! Please provide your
data
withdput(data)
to make a MCVE. Thanks!– jay.sf
Nov 21 '18 at 18:20
Hi & welcome to Stack Overflow! Please provide your
data
withdput(data)
to make a MCVE. Thanks!– jay.sf
Nov 21 '18 at 18:20
ggplot
is powerful when you feed it long data, but unwieldy with wide data. I suggest you add a step before ggplot where you convert it, e.g. tidyr::gather(element, value, La:Lu)
...– Jon Spring
Nov 21 '18 at 19:16
ggplot
is powerful when you feed it long data, but unwieldy with wide data. I suggest you add a step before ggplot where you convert it, e.g. tidyr::gather(element, value, La:Lu)
...– Jon Spring
Nov 21 '18 at 19:16
1
1
Hi @jay.sf I have changed this - thanks for the tip!
– lmm
Nov 21 '18 at 21:11
Hi @jay.sf I have changed this - thanks for the tip!
– lmm
Nov 21 '18 at 21:11
@jay.sf I just saw it - thank you!
– lmm
Nov 22 '18 at 14:33
@jay.sf I just saw it - thank you!
– lmm
Nov 22 '18 at 14:33
add a comment |
1 Answer
1
active
oldest
votes
You probably need to transpose your data, which is different in your example compared to the linked example.
data <- as.data.frame(t(data)) # transpose your data here with `t()`
data$id <- 1:nrow(data)
library(reshape2)
plot_data <- melt(data,id.var="id")
library(ggplot2)
ggplot(plot_data, aes(x=id, y=value, group=variable, colour=variable)) +
geom_point() +
geom_line(aes(lty=variable))
Yields
1
Thank you!! This has worked perfectly.
– lmm
Nov 22 '18 at 14:33
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%2f53418301%2fggplot-lineplot-for-multiple-rows-of-data-ggplot-equivalent-for-matplot%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
You probably need to transpose your data, which is different in your example compared to the linked example.
data <- as.data.frame(t(data)) # transpose your data here with `t()`
data$id <- 1:nrow(data)
library(reshape2)
plot_data <- melt(data,id.var="id")
library(ggplot2)
ggplot(plot_data, aes(x=id, y=value, group=variable, colour=variable)) +
geom_point() +
geom_line(aes(lty=variable))
Yields
1
Thank you!! This has worked perfectly.
– lmm
Nov 22 '18 at 14:33
add a comment |
You probably need to transpose your data, which is different in your example compared to the linked example.
data <- as.data.frame(t(data)) # transpose your data here with `t()`
data$id <- 1:nrow(data)
library(reshape2)
plot_data <- melt(data,id.var="id")
library(ggplot2)
ggplot(plot_data, aes(x=id, y=value, group=variable, colour=variable)) +
geom_point() +
geom_line(aes(lty=variable))
Yields
1
Thank you!! This has worked perfectly.
– lmm
Nov 22 '18 at 14:33
add a comment |
You probably need to transpose your data, which is different in your example compared to the linked example.
data <- as.data.frame(t(data)) # transpose your data here with `t()`
data$id <- 1:nrow(data)
library(reshape2)
plot_data <- melt(data,id.var="id")
library(ggplot2)
ggplot(plot_data, aes(x=id, y=value, group=variable, colour=variable)) +
geom_point() +
geom_line(aes(lty=variable))
Yields
You probably need to transpose your data, which is different in your example compared to the linked example.
data <- as.data.frame(t(data)) # transpose your data here with `t()`
data$id <- 1:nrow(data)
library(reshape2)
plot_data <- melt(data,id.var="id")
library(ggplot2)
ggplot(plot_data, aes(x=id, y=value, group=variable, colour=variable)) +
geom_point() +
geom_line(aes(lty=variable))
Yields
answered Nov 21 '18 at 21:21
jay.sfjay.sf
5,26221639
5,26221639
1
Thank you!! This has worked perfectly.
– lmm
Nov 22 '18 at 14:33
add a comment |
1
Thank you!! This has worked perfectly.
– lmm
Nov 22 '18 at 14:33
1
1
Thank you!! This has worked perfectly.
– lmm
Nov 22 '18 at 14:33
Thank you!! This has worked perfectly.
– lmm
Nov 22 '18 at 14:33
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%2f53418301%2fggplot-lineplot-for-multiple-rows-of-data-ggplot-equivalent-for-matplot%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
Hi & welcome to Stack Overflow! Please provide your
data
withdput(data)
to make a MCVE. Thanks!– jay.sf
Nov 21 '18 at 18:20
ggplot
is powerful when you feed it long data, but unwieldy with wide data. I suggest you add a step before ggplot where you convert it, e.g.tidyr::gather(element, value, La:Lu)
...– Jon Spring
Nov 21 '18 at 19:16
1
Hi @jay.sf I have changed this - thanks for the tip!
– lmm
Nov 21 '18 at 21:11
@jay.sf I just saw it - thank you!
– lmm
Nov 22 '18 at 14:33