ggplot lineplot for multiple rows of data - ggplot equivalent for matplot











up vote
1
down vote

favorite












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?










share|improve this question
























  • Hi & welcome to Stack Overflow! Please provide your data withdput(data) to make a MCVE. Thanks!
    – jay.sf
    Nov 21 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 at 19:16






  • 1




    Hi @jay.sf I have changed this - thanks for the tip!
    – lmm
    Nov 21 at 21:11










  • @jay.sf I just saw it - thank you!
    – lmm
    Nov 22 at 14:33















up vote
1
down vote

favorite












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?










share|improve this question
























  • Hi & welcome to Stack Overflow! Please provide your data withdput(data) to make a MCVE. Thanks!
    – jay.sf
    Nov 21 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 at 19:16






  • 1




    Hi @jay.sf I have changed this - thanks for the tip!
    – lmm
    Nov 21 at 21:11










  • @jay.sf I just saw it - thank you!
    – lmm
    Nov 22 at 14:33













up vote
1
down vote

favorite









up vote
1
down vote

favorite











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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 at 21:26









jay.sf

4,11921435




4,11921435










asked Nov 21 at 18:18









lmm

84




84












  • Hi & welcome to Stack Overflow! Please provide your data withdput(data) to make a MCVE. Thanks!
    – jay.sf
    Nov 21 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 at 19:16






  • 1




    Hi @jay.sf I have changed this - thanks for the tip!
    – lmm
    Nov 21 at 21:11










  • @jay.sf I just saw it - thank you!
    – lmm
    Nov 22 at 14:33


















  • Hi & welcome to Stack Overflow! Please provide your data withdput(data) to make a MCVE. Thanks!
    – jay.sf
    Nov 21 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 at 19:16






  • 1




    Hi @jay.sf I have changed this - thanks for the tip!
    – lmm
    Nov 21 at 21:11










  • @jay.sf I just saw it - thank you!
    – lmm
    Nov 22 at 14:33
















Hi & welcome to Stack Overflow! Please provide your data withdput(data) to make a MCVE. Thanks!
– jay.sf
Nov 21 at 18:20




Hi & welcome to Stack Overflow! Please provide your data withdput(data) to make a MCVE. Thanks!
– jay.sf
Nov 21 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 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 at 19:16




1




1




Hi @jay.sf I have changed this - thanks for the tip!
– lmm
Nov 21 at 21:11




Hi @jay.sf I have changed this - thanks for the tip!
– lmm
Nov 21 at 21:11












@jay.sf I just saw it - thank you!
– lmm
Nov 22 at 14:33




@jay.sf I just saw it - thank you!
– lmm
Nov 22 at 14:33












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










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
enter image description here






share|improve this answer

















  • 1




    Thank you!! This has worked perfectly.
    – lmm
    Nov 22 at 14:33











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',
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
});


}
});














draft saved

draft discarded


















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








up vote
0
down vote



accepted










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
enter image description here






share|improve this answer

















  • 1




    Thank you!! This has worked perfectly.
    – lmm
    Nov 22 at 14:33















up vote
0
down vote



accepted










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
enter image description here






share|improve this answer

















  • 1




    Thank you!! This has worked perfectly.
    – lmm
    Nov 22 at 14:33













up vote
0
down vote



accepted







up vote
0
down vote



accepted






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
enter image description here






share|improve this answer












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
enter image description here







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 at 21:21









jay.sf

4,11921435




4,11921435








  • 1




    Thank you!! This has worked perfectly.
    – lmm
    Nov 22 at 14:33














  • 1




    Thank you!! This has worked perfectly.
    – lmm
    Nov 22 at 14:33








1




1




Thank you!! This has worked perfectly.
– lmm
Nov 22 at 14:33




Thank you!! This has worked perfectly.
– lmm
Nov 22 at 14:33


















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)