I’m trying to get a barplot in R to represent the number of occurrences of a target variable from a csv file. However, the output barplot I get is a fully black pictures with lines in between. What’s going on with the graph and how do I get this fixed? Thank you
structure(list(ID = c(140L, 620L, 868L, 1120L, 2313L), DemAffl = c(10L, 4L, 5L, 10L, 11L), DemAge = c(76L, 49L, 70L, 65L, 68L), DemCluster = c(16L, 35L, 27L, 51L, 4L), DemClusterGroup = c("C", "D", "D", "F", "A" ), DemGender = c("U", "U", "F", "M", "F"), DemReg = c("Midlands", "Midlands", "Midlands", "Midlands", "Midlands"), DemTVReg = c("Wales & West", "Wales & West", "Wales & West", "Midlands", "Midlands"), PromClass = c("Gold", "Gold", "Silver", "Tin", "Tin"), PromSpend = c(16000, 6000, 0.02, 0.01, 0.01), PromTime = c(4L, 5L, 8L, 7L, 8L), TargetBuy = c(0L, 0L, 1L, 1L, 0L), TargetAmt = c(0L, 0L, 1L, 1L, 0L)), row.names = c(NA, 5L), class = "data.frame")
Your barplot code is working as it should be; since your graphed variable is binary, there’s a good chance that the white stripes are where the target variable was "0". When the dataframe has a huge number of rows, R’s barplot() function will remove most of the traditional formatting to make each column a single black line.
In addition, when passing only a single row of data into barplot, a visualization with two axis, it will automatically make the x-axis the index of the data and the y-axis the actual data value.
Your code line barplot(organics.df$TargetBuy) will make a barplot where your x-axis is the index/position of a value in the dataframe and the y-axis is that value. In this case, it seems your values are ranging from 0-1, with 0s appearing as straight white lines and long black rectangles representing large sections of the dataframe where your graphed variable is 1.
This will cause your graph to look like this, given a dataframe of 1s and 0s:
This is the graph with only 15 observations. Crank this up to several thousand and it will look very similar to the picture you submitted.
To answer your original question, if you would like a bar plot to show the number of occurrences of each type, use this code:
counts <- table(organics.df$TargetBuy) barplot(counts, main= "number of yes/no", xlab = "response", ylab = "number of occurrences")
You should get a graph similar to this: