문제

I am looking for the most convenient way of creating boxplots for different values and groups read from a CSV file in R.

First, I read my Sheet into memory:

Sheet <- read.csv("D:/mydata/Table.csv",  sep = ";")

Which just works fine.

names(Sheet) 

gives me correctly the Headlines of the different columns.

I can also access and filter different groups into separate lists, like

myData1 <- Sheet[Sheet$Group == 'Group1',]$MyValue
myData2 <- Sheet[Sheet$Group == 'Group2',]$MyValue
...

and draw a boxplot using

boxplot(myData1, myData2, ..., main = "Distribution")

where the ... stand for more lists I have filled using the selection method above.

However, I have seen that using some formular could do these steps of selection and boxplotting in one go. But when I use something like

boxplot(Sheet~Group, Sheet)

it won't work because I get the following error:

invalid type (list) for variable 'Sheet'

The data in the CSV looks like this:

No;Gender;Type;Volume;Survival
1;m;HCM;150;45
2;m;UCM;202;103
3;f;HCM;192;5
4;m;T4;204;101
...

So i have multiple possible groups and different values which I'd like to represent as a box plot for each group. For example, I could group by gender or group by type.

How can I easily draw multiple boxes from my CSV data without having to grab them all manually out of the data?

Thanks for your help.

도움이 되었습니까?

해결책

Try it like this:

Sheet <- data.frame(Group = gl(2, 50, labels=c("Group1", "Group2")),
                    MyValue = runif(100))
boxplot(MyValue ~ Group, data=Sheet)

다른 팁

Using ggplot2:

ggplot(Sheet, aes(x = Group, y = MyValue)) +
  geom_boxplot()

The advantage of using ggplot2 is that you have lots of possibilities for customizing the appearance of your boxplot.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top