Trying to create a plot

Im trying to create a scatterplot and I'm getting an error and can't figure out how to correct it. please help

chd.wt<-matrix(c(3, 2.9, 3.1, 4, 6, 1.1, 1.2, 2, 3, 3.8, 1, 1.2, 1.1, 1.1, 3.2, 1, 1.2, 1.2, 1, 2, 0, 0, 0, 0.8, 2), byrow = T, ncol = 5)
colnames(chd.wt)<-c("<19", "19-20.3", "20.4-21.5", "21.6-23.2", ">23.3")
rownames(chd.wt)<-c("gained 20 kg", "gained 11-20 kg", "gained 5-11 kg", "lost 5 to gained 5 kg", "lost 5+ kg")
xyplot(colnames(chd.wt) ~ row.names(chd.wt), data=chd.wt, type=c("T", "5"), auto.key=T, ylab="BMI at 18", xlab="Change in weight")
Error in eval(substitute(groups), data, environment(x)) :
numeric 'envir' arg not of length one

A scatterplot are just points placed on a plane based on (x, y) coordinates provided, and doesn't map any value to those points, you are not maping any variable to the x and y axes and if I correctly understand your problem, even if you reshape your data into a long format those axis would be categorical (scatterplots need numerical scales), this is the closest I can get to a scatterplot using your data and including the BMI values.

chd.wt<-matrix(c(3, 2.9, 3.1, 4, 6, 1.1, 1.2, 2, 3, 3.8, 1, 1.2, 1.1, 1.1, 3.2, 1, 1.2, 1.2, 1, 2, 0, 0, 0, 0.8, 2), byrow = T, ncol = 5)
colnames(chd.wt)<-c("<19", "19-20.3", "20.4-21.5", "21.6-23.2", ">23.3")
rownames(chd.wt)<-c("gained 20 kg", "gained 11-20 kg", "gained 5-11 kg", "lost 5 to gained 5 kg", "lost 5+ kg")

library(tidyverse)
as_tibble(chd.wt) %>% 
    bind_cols("weight" = rownames(chd.wt)) %>% 
    gather(age, BMI, -weight) %>% 
    mutate(age = factor(age, levels = c("<19", "19-20.3", "20.4-21.5", "21.6-23.2", ">23.3")),
           weight = factor(weight, levels = c("gained 20 kg", "gained 11-20 kg", "gained 5-11 kg", "lost 5 to gained 5 kg", "lost 5+ kg"))
           ) %>% 
    ggplot(aes(x = age, y = fct_rev(weight))) +
    geom_point(aes(color = BMI, size = BMI)) +
    scale_color_continuous(guide = "legend") +
    labs(x = "Age Range",
         y = "Weight Variation",
         color = "BMI", 
         size = "BMI") +
    theme(axis.text.x = element_text(angle=30, hjust=1, vjust = 1))

Created on 2019-07-04 by the reprex package (v0.3.0)

EDIT: I think geom_tile() looks better in this case

as_tibble(chd.wt) %>% 
    bind_cols("weight" = rownames(chd.wt)) %>% 
    gather(age, BMI, -weight) %>% 
    mutate(age = factor(age, levels = c("<19", "19-20.3", "20.4-21.5", "21.6-23.2", ">23.3")),
           weight = factor(weight, levels = c("gained 20 kg", "gained 11-20 kg", "gained 5-11 kg", "lost 5 to gained 5 kg", "lost 5+ kg"))
    ) %>% 
    ggplot(aes(x = age, y = fct_rev(weight))) +
    geom_tile(aes(fill = BMI)) +
    labs(x = "Age Range",
         y = "Weight Variation",
         fill = "BMI") +
    theme(axis.text.x = element_text(angle=30, hjust=1, vjust = 1))

2 Likes

I'm still new to R studio. The tidyverse package, how is it different from mosaic, and do both packages work together or no? I saw that mosaic and tidyverse had a conflict. Is that something I should worry about?

I also modified it a bit

chd.wt<-matrix(c(3, 2.9, 3.1, 4, 6, 1.1, 1.2, 2, 3, 3.8, 1, 1.2, 1.1, 1.1, 3.2, 1, 1.2, 1.2, 1, 2, 0, 0, 0, 0.8, 2), byrow = T, ncol = 5)
colnames(chd.wt)<-c("<19", "19-20.3", "20.4-21.5", "21.6-23.2", ">23.3")
rownames(chd.wt)<-c("gained 20 kg", "gained 11-20 kg", "gained 5-11 kg", "lost 5 to gained 5 kg", "lost 5+ kg")

library(tidyverse)
as_tibble(chd.wt) %>% 
bind_cols("weight" = rownames(chd.wt)) %>% 
gather(mass, BMI, -weight) %>% 
mutate(mass = factor(mass, levels = c("<19", "19-20.3", "20.4-21.5", "21.6-23.2", ">23.3")),
weight = factor(weight, levels = c("gained 20 kg", "gained 11-20 kg", "gained 5-11 kg", "lost 5 to gained 5 kg", "lost 5+ kg"))) %>% 
ggplot(aes(x = mass, y = fct_rev(weight))) +
geom_point(aes(color = BMI, size = BMI)) +
scale_color_continuous(guide = "legend") +
labs(x = "BMI Range", y = "Weight Variation", color = "RR", size = "RR") +
theme(axis.text.x = element_text(angle=30, hjust=1, vjust = 1))

Rplot%20for%20Assignment%204

I have never used mosaic package so I have no idea, if you have an specific problem working with it you should ask about it on a new topic providing a relevant reproducible example

Your data is well set up for some of the base plotting methods, like mosaicplot and image:

chd.wt<-matrix(c(3, 2.9, 3.1, 4, 6, 1.1, 1.2, 2, 3, 3.8, 1, 1.2, 1.1, 1.1, 3.2, 1, 1.2, 1.2, 1, 2, 0, 0, 0, 0.8, 2), byrow = T, ncol = 5)
colnames(chd.wt)<-c("<19", "19-20.3", "20.4-21.5", "21.6-23.2", ">23.3")
rownames(chd.wt)<-c("gained 20 kg", "gained 11-20 kg", "gained 5-11 kg", "lost 5 to gained 5 kg", "lost 5+ kg")

mosaicplot(chd.wt)

image(chd.wt)

They'd take some tweaking to be pretty, though, and tweaking base graphics can be painful, so I'd honestly use ggplot like the answer above.

This is the first time learning about ggplot and I think I'll play around with that. thank you

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.