Skip to content

hist from a list

2 messages · Pedro páramo, Rui Barradas

#
Hi Rasmus, Josh and Rui,

First of all many thanks in advance about your help.

The first thig is sometimes you say " you are posting in HTML and that
makes the
post unreadable as this is a plain text list" how can I put the code in the
correct way, not html (attaching in txt?)

The second about the code:

I have used this:

bwc <- cbind(bwfinal2,bwfinal)
colnames(bwc)=c("Accion","reval")
df <- matrix(unlist(bwc), nrow=nrow(bwc), byrow=F)
colnames(bwchist)=c("Accion","reval")
bwchist <-as.data.frame(bwc[order(df[,2]), ])

bwchist is the ordered cum stock returns in the year but because is a list
it is not possible to plot and histogram with x (names of stocks) and the x
axist the value of cum stocks (reval)

when I put dput(bwchist) the console says:

dput(bwchist)
structure(list(Accion = list("REE", "Enagas", "Grifols", "Ferrovial",
    "Acerinox", "Naturgy", "Inditex", "Bankia", "ENCE", "Aena",
    "Bankinter", "Mapfre", "CaixaBank", "CIE", "Colonial", "Almirall",
    "Indra", "ArcelorMittal", "ACS", "Telefonica", "Amadeus",
    "BBVA", "Merlin", "Santander", "Repsol", "Melia", "Sabadell",
    "IAG", "Acciona", "Endesa", "MasMovil", "Iberdrola", "SGamesa",
    "Viscofan", "Cellnex"), reval = list(-0.0200827282700085,
    -0.0590294115600855, -0.214126598790964, -0.220773677809979,
    -0.229653300324357, -0.257944379583984, -0.283942789063822,
    -0.285159347392533, -0.303814713896458, -0.30734460425763,
    -0.309408155539818, -0.319912221435868, -0.322790949659181,
    -0.344047579452905, -0.347919538415482, -0.356898907103825,
    -0.374263261296661, -0.40147247119078, -0.405150043834815,
    -0.406022775042175, -0.413786100987797, -0.440679109311707,
    -0.442603156492871, -0.491634140733524, -0.499254932434042,
    -0.6, -0.709737357505148, -0.724461258850966, 0.0220528711420083,
    0.0462767672643172, 0.115044247787611, 0.238734548714937,
    0.274578114644054, 0.343422896082666, 0.387826126094928)), class =
"data.frame", row.names = c(NA,
-35L))

I try to make an hist or barplot but because it is a list no way to obtain
the plot.

Many thanks again for your help.

I have printed two manuals to improve my level, but if you can help me, I
would be very very gratefull.



El vie., 31 jul. 2020 a las 18:28, Rasmus Liland (<jral at posteo.no>)
escribi?:

  
  
#
Hello,

Thanks for the data in dput format.
If you run

str(bwchist)

you will see that what you have is a data.frame, yes, but, with columns 
of class "list", not vectors.
So the first step is to make them vectors, to unlist the lists. I will 
do it applying function unlist() to each of the columns. Since lapply 
returns a list, I remake a data.frame. The original is kept unchanged, 
the new object is bwch.

bwch <- lapply(bwchist, unlist, recursive = FALSE)
bwch <- do.call(cbind.data.frame, bwch)
str(bwch)


Now that everything is as it should, here are two ways of plotting bar 
graphs.

#--- base R
x11(width = 11.5, height = 6)
old_par <- par(mar =? par("mar") + c(1, 0, 0, 0))
bp <- barplot(bwch$reval, yaxt = "n", ylim = c(-1, 0.4))
axis(1, at = bp, labels = bwch$Accion, las = 2)
axis(2, at = pretty(bwch$reval))
par(old_par)


#--- package ggplot2
library(ggplot2)

x11(width = 11.5, height = 6)
ggplot(bwch, aes(factor(Accion, levels = Accion), reval)) +
 ? geom_col() +
 ? theme(axis.text.x = element_text(angle = 60, hjust = 1))


Hope this helps,

Rui Barradas

?s 19:48 de 03/08/2020, Pedro p?ramo escreveu: