How to create a bwplot in lattice

1

I'm just going through my adventure with R and I still have many doubts. I would like to create a bwplot of the 5 subsets that I have but I have no idea how to do it. I want to compare variables RealInv and RealGNP .

It is what is at the moment and I need to visualize it in some way in lattice:

library(lattice)
n<- 4
dfchunk <- split(Investment, factor(sort(rank(row.names(Investment))%%n)))
dfchunk

These are the starting data:

Investment <- structure(list(GNP = c(596.7, 637.7, 691.1, 756, 799.6, 873.4 ), Investment = c(90.9, 97.4, 113.5, 125.7, 122.8, 133.3), Price = c(0.7167, 0.7277, 0.7436, 0.7676, 0.7906, 0.8254), Interest = c(3.23, 3.55, 4.04, 4.5, 4.19, 5.16), RealGNP = c(832.565927166178, 876.322660436993, 929.397525551372, 984.887962480459, 1011.38375917025, 1058.15362248607 ), RealInv = c(126.83131017162, 133.84636526041, 152.635825712749, 163.757165190203, 155.325069567417, 161.497455779016),RealInt = c(NA, 2.01518766568997, 1.85503366772021, 1.27245831091986, 1.19364773319437, 0.758279787503155)), .Names = c("GNP", "Investment", "Price", "Interest", "RealGNP", "RealInv", "RealInt"), row.names = c(NA, 6L), class = "data.frame")

I'll be grateful for any indication

    
asked by Kas 26.11.2017 в 18:45
source

2 answers

1

With ggplot it can be done like this

Investment <- structure(list(GNP = c(596.7, 637.7, 691.1, 756, 799.6, 873.4 ), Investment = c(90.9, 97.4, 113.5, 125.7, 122.8, 133.3), Price = c(0.7167, 0.7277, 0.7436, 0.7676, 0.7906, 0.8254), Interest = c(3.23, 3.55, 4.04, 4.5, 4.19, 5.16), RealGNP = c(832.565927166178, 876.322660436993, 929.397525551372, 984.887962480459, 1011.38375917025, 1058.15362248607 ), RealInv = c(126.83131017162, 133.84636526041, 152.635825712749, 163.757165190203, 155.325069567417, 161.497455779016),RealInt = c(NA, 2.01518766568997, 1.85503366772021, 1.27245831091986, 1.19364773319437, 0.758279787503155)), .Names = c("GNP", "Investment", "Price", "Interest", "RealGNP", "RealInv", "RealInt"), row.names = c(NA, 6L), class = "data.frame")

library(tidyverse)                  #Carga librería de gráficos y manipulación. 
Investment %>%                      #llamo a los datos
  select(RealGNP, RealInv) %>%      #Selecciono las columnas que me interesan
  gather() %>%                      #Las paso a formato largo
  ggplot(aes(x=key, y=value)) +     #Mapeo las variables a los ejes.
  geom_boxplot()                    #Especifico el tipo de gráfico. 
    
answered by 27.11.2017 в 18:00
0

As you commented mpaladino already in your comments the boxplot serve to compare categorical variables, discrete or Factor , as you want to call them. For what you comment, what you're looking for is to compare RealInv with RealGNP , so you should create these two categories. For example, if we took your complete example, you could do this:

Investment <- structure(list(GNP = c(596.7, 637.7, 691.1, 756, 799.6, 873.4 ), Investment = c(90.9, 97.4, 113.5, 125.7, 122.8, 133.3), Price = c(0.7167, 0.7277, 0.7436, 0.7676, 0.7906, 0.8254), Interest = c(3.23, 3.55, 4.04, 4.5, 4.19, 5.16), RealGNP = c(832.565927166178, 876.322660436993, 929.397525551372, 984.887962480459, 1011.38375917025, 1058.15362248607 ), RealInv = c(126.83131017162, 133.84636526041, 152.635825712749, 163.757165190203, 155.325069567417, 161.497455779016),RealInt = c(NA, 2.01518766568997, 1.85503366772021, 1.27245831091986, 1.19364773319437, 0.758279787503155)), .Names = c("GNP", "Investment", "Price", "Interest", "RealGNP", "RealInv", "RealInt"), row.names = c(NA, 6L), class = "data.frame")

df <- data.frame(item=c(rep("RealGNP",times=nrow(Investment)), rep("RealInv",times=nrow(Investment))),
                 valor=c(Investment$RealGNP,Investment$RealInv))

To graph we have created a new data.frame with the following structure:

      item     valor
1  RealGNP  832.5659
2  RealGNP  876.3227
3  RealGNP  929.3975
4  RealGNP  984.8880
5  RealGNP 1011.3838
6  RealGNP 1058.1536
7  RealInv  126.8313
8  RealInv  133.8464
9  RealInv  152.6358
10 RealInv  163.7572
11 RealInv  155.3251
12 RealInv  161.4975

Now, we graph these values:

bwplot(valor ~ item, data=df)

And the exit:

You can also apply it to your chunks of data, in short they are not more than other data.frames but what you have to keep in mind, that in your example dfchunk is a list of data.frames , by what should plotear each accessing by index, for example:

chunk <- dfchunk[[2]]
df <- data.frame(item=c(rep("RealGNP",times=nrow(chunk)), rep("RealInv",times=nrow(chunk))),
                 valor=c(chunk$RealGNP,chunk$RealInv))

bwplot(valor ~ item, data=df)

Exit:

    
answered by 27.11.2017 в 16:08