plot grouped data in R
I have a grouped data as follows:
group x y
group1 0 5
group4 0 5
group1 7 5
group4 0 5
group5 7 5
group1 7 5
group1 0 6
group2 0 6
group4 0 5
group2 0 5
group3 7 5
both x and y are discrete values having ranging between 0 and 7. I want to get a plot place each group data on the x-y plane according to their respective x and y values.For example, I can have multiple group1 points, all of which should share the same color. How to do that in R?
r graph plot
add a comment |
I have a grouped data as follows:
group x y
group1 0 5
group4 0 5
group1 7 5
group4 0 5
group5 7 5
group1 7 5
group1 0 6
group2 0 6
group4 0 5
group2 0 5
group3 7 5
both x and y are discrete values having ranging between 0 and 7. I want to get a plot place each group data on the x-y plane according to their respective x and y values.For example, I can have multiple group1 points, all of which should share the same color. How to do that in R?
r graph plot
add a comment |
I have a grouped data as follows:
group x y
group1 0 5
group4 0 5
group1 7 5
group4 0 5
group5 7 5
group1 7 5
group1 0 6
group2 0 6
group4 0 5
group2 0 5
group3 7 5
both x and y are discrete values having ranging between 0 and 7. I want to get a plot place each group data on the x-y plane according to their respective x and y values.For example, I can have multiple group1 points, all of which should share the same color. How to do that in R?
r graph plot
I have a grouped data as follows:
group x y
group1 0 5
group4 0 5
group1 7 5
group4 0 5
group5 7 5
group1 7 5
group1 0 6
group2 0 6
group4 0 5
group2 0 5
group3 7 5
both x and y are discrete values having ranging between 0 and 7. I want to get a plot place each group data on the x-y plane according to their respective x and y values.For example, I can have multiple group1 points, all of which should share the same color. How to do that in R?
r graph plot
r graph plot
edited Mar 21 '13 at 12:17
Arun
93.6k11218319
93.6k11218319
asked Mar 21 '13 at 11:38
user297850user297850
2,593103450
2,593103450
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
The data:
dat <- read.table(text = "group x y
group1 0 5
group4 0 5
group1 7 5
group4 0 5
group5 7 5
group1 7 5
group1 0 6
group2 0 6
group4 0 5
group2 0 5
group3 7 5", header = TRUE)
You can use the excellent ggplot2
package for easy plotting:
library(ggplot2)
ggplot(dat, aes(x = x, y = y, colour = group)) +
geom_point() +
facet_wrap( ~ group)
Here, I used facet_wrap
to create facets for each group. In principle this is not necessary, since the groups' points can be distinguished by their colour. But in this case there are only three different locations at the figure. Hence, not all points would be visible if the data were plotted in a single scatterplot.
add a comment |
Using the data from Sven's answer, you can also look at the lattice package, which should already be installed with your R installation:
library(lattice)
# Each group in a separate mini plot
xyplot(y ~ x | group, data = dat)
# All groups in one plot, different colors for each group
# Not at all interesting with the example data you've provided
xyplot(y ~ x, groups=dat$group, data = dat)
Here's an example of each with a little bit more data:
set.seed(1)
mydf <- data.frame(
group = sample(letters[1:4], 50, replace = TRUE),
x = runif(50, 0, 7),
y = runif(50, 0, 7)
)
xyplot(y ~ x, groups=mydf$group, data = mydf,
auto.key = list(corner = c(0, .98)), cex = 1.5)
xyplot(y ~ x | group, data = mydf,
auto.key = list(corner = c(0, .98)), cex = 1.5)
add a comment |
dd<- read.table("put the path to the txt file containing the data here", header=TRUE)
g <- ggplot(dd, aes(as.factor(group)))
g <- g + geom_point(aes(y=x), colour="red")
g <- g + geom_point(aes(y=y), colour="green")
g
This would give you x and y as a function of the group, in a single plot, as shown below.
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%2f15546468%2fplot-grouped-data-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
The data:
dat <- read.table(text = "group x y
group1 0 5
group4 0 5
group1 7 5
group4 0 5
group5 7 5
group1 7 5
group1 0 6
group2 0 6
group4 0 5
group2 0 5
group3 7 5", header = TRUE)
You can use the excellent ggplot2
package for easy plotting:
library(ggplot2)
ggplot(dat, aes(x = x, y = y, colour = group)) +
geom_point() +
facet_wrap( ~ group)
Here, I used facet_wrap
to create facets for each group. In principle this is not necessary, since the groups' points can be distinguished by their colour. But in this case there are only three different locations at the figure. Hence, not all points would be visible if the data were plotted in a single scatterplot.
add a comment |
The data:
dat <- read.table(text = "group x y
group1 0 5
group4 0 5
group1 7 5
group4 0 5
group5 7 5
group1 7 5
group1 0 6
group2 0 6
group4 0 5
group2 0 5
group3 7 5", header = TRUE)
You can use the excellent ggplot2
package for easy plotting:
library(ggplot2)
ggplot(dat, aes(x = x, y = y, colour = group)) +
geom_point() +
facet_wrap( ~ group)
Here, I used facet_wrap
to create facets for each group. In principle this is not necessary, since the groups' points can be distinguished by their colour. But in this case there are only three different locations at the figure. Hence, not all points would be visible if the data were plotted in a single scatterplot.
add a comment |
The data:
dat <- read.table(text = "group x y
group1 0 5
group4 0 5
group1 7 5
group4 0 5
group5 7 5
group1 7 5
group1 0 6
group2 0 6
group4 0 5
group2 0 5
group3 7 5", header = TRUE)
You can use the excellent ggplot2
package for easy plotting:
library(ggplot2)
ggplot(dat, aes(x = x, y = y, colour = group)) +
geom_point() +
facet_wrap( ~ group)
Here, I used facet_wrap
to create facets for each group. In principle this is not necessary, since the groups' points can be distinguished by their colour. But in this case there are only three different locations at the figure. Hence, not all points would be visible if the data were plotted in a single scatterplot.
The data:
dat <- read.table(text = "group x y
group1 0 5
group4 0 5
group1 7 5
group4 0 5
group5 7 5
group1 7 5
group1 0 6
group2 0 6
group4 0 5
group2 0 5
group3 7 5", header = TRUE)
You can use the excellent ggplot2
package for easy plotting:
library(ggplot2)
ggplot(dat, aes(x = x, y = y, colour = group)) +
geom_point() +
facet_wrap( ~ group)
Here, I used facet_wrap
to create facets for each group. In principle this is not necessary, since the groups' points can be distinguished by their colour. But in this case there are only three different locations at the figure. Hence, not all points would be visible if the data were plotted in a single scatterplot.
answered Mar 21 '13 at 11:47
Sven HohensteinSven Hohenstein
66.1k12100131
66.1k12100131
add a comment |
add a comment |
Using the data from Sven's answer, you can also look at the lattice package, which should already be installed with your R installation:
library(lattice)
# Each group in a separate mini plot
xyplot(y ~ x | group, data = dat)
# All groups in one plot, different colors for each group
# Not at all interesting with the example data you've provided
xyplot(y ~ x, groups=dat$group, data = dat)
Here's an example of each with a little bit more data:
set.seed(1)
mydf <- data.frame(
group = sample(letters[1:4], 50, replace = TRUE),
x = runif(50, 0, 7),
y = runif(50, 0, 7)
)
xyplot(y ~ x, groups=mydf$group, data = mydf,
auto.key = list(corner = c(0, .98)), cex = 1.5)
xyplot(y ~ x | group, data = mydf,
auto.key = list(corner = c(0, .98)), cex = 1.5)
add a comment |
Using the data from Sven's answer, you can also look at the lattice package, which should already be installed with your R installation:
library(lattice)
# Each group in a separate mini plot
xyplot(y ~ x | group, data = dat)
# All groups in one plot, different colors for each group
# Not at all interesting with the example data you've provided
xyplot(y ~ x, groups=dat$group, data = dat)
Here's an example of each with a little bit more data:
set.seed(1)
mydf <- data.frame(
group = sample(letters[1:4], 50, replace = TRUE),
x = runif(50, 0, 7),
y = runif(50, 0, 7)
)
xyplot(y ~ x, groups=mydf$group, data = mydf,
auto.key = list(corner = c(0, .98)), cex = 1.5)
xyplot(y ~ x | group, data = mydf,
auto.key = list(corner = c(0, .98)), cex = 1.5)
add a comment |
Using the data from Sven's answer, you can also look at the lattice package, which should already be installed with your R installation:
library(lattice)
# Each group in a separate mini plot
xyplot(y ~ x | group, data = dat)
# All groups in one plot, different colors for each group
# Not at all interesting with the example data you've provided
xyplot(y ~ x, groups=dat$group, data = dat)
Here's an example of each with a little bit more data:
set.seed(1)
mydf <- data.frame(
group = sample(letters[1:4], 50, replace = TRUE),
x = runif(50, 0, 7),
y = runif(50, 0, 7)
)
xyplot(y ~ x, groups=mydf$group, data = mydf,
auto.key = list(corner = c(0, .98)), cex = 1.5)
xyplot(y ~ x | group, data = mydf,
auto.key = list(corner = c(0, .98)), cex = 1.5)
Using the data from Sven's answer, you can also look at the lattice package, which should already be installed with your R installation:
library(lattice)
# Each group in a separate mini plot
xyplot(y ~ x | group, data = dat)
# All groups in one plot, different colors for each group
# Not at all interesting with the example data you've provided
xyplot(y ~ x, groups=dat$group, data = dat)
Here's an example of each with a little bit more data:
set.seed(1)
mydf <- data.frame(
group = sample(letters[1:4], 50, replace = TRUE),
x = runif(50, 0, 7),
y = runif(50, 0, 7)
)
xyplot(y ~ x, groups=mydf$group, data = mydf,
auto.key = list(corner = c(0, .98)), cex = 1.5)
xyplot(y ~ x | group, data = mydf,
auto.key = list(corner = c(0, .98)), cex = 1.5)
edited Mar 21 '13 at 12:13
answered Mar 21 '13 at 11:55
A5C1D2H2I1M1N2O1R2T1A5C1D2H2I1M1N2O1R2T1
154k18287381
154k18287381
add a comment |
add a comment |
dd<- read.table("put the path to the txt file containing the data here", header=TRUE)
g <- ggplot(dd, aes(as.factor(group)))
g <- g + geom_point(aes(y=x), colour="red")
g <- g + geom_point(aes(y=y), colour="green")
g
This would give you x and y as a function of the group, in a single plot, as shown below.
add a comment |
dd<- read.table("put the path to the txt file containing the data here", header=TRUE)
g <- ggplot(dd, aes(as.factor(group)))
g <- g + geom_point(aes(y=x), colour="red")
g <- g + geom_point(aes(y=y), colour="green")
g
This would give you x and y as a function of the group, in a single plot, as shown below.
add a comment |
dd<- read.table("put the path to the txt file containing the data here", header=TRUE)
g <- ggplot(dd, aes(as.factor(group)))
g <- g + geom_point(aes(y=x), colour="red")
g <- g + geom_point(aes(y=y), colour="green")
g
This would give you x and y as a function of the group, in a single plot, as shown below.
dd<- read.table("put the path to the txt file containing the data here", header=TRUE)
g <- ggplot(dd, aes(as.factor(group)))
g <- g + geom_point(aes(y=x), colour="red")
g <- g + geom_point(aes(y=y), colour="green")
g
This would give you x and y as a function of the group, in a single plot, as shown below.
answered Mar 22 '13 at 11:36
Venkatramanan P.R.Venkatramanan P.R.
843
843
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%2f15546468%2fplot-grouped-data-in-r%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