R stacked bar plot with continuous scale - r

I'm trying to create a stacked bar plot to indicate when requests were made for resources on website. I would like to use a gradient legend to describe when the requests were made. How can I create a gradient legend, and would that be the right way to visualize this time domain data?
> head(livePostHit)
path date hits
1 /2017/06/27/goog-fit-cal.html 2018/04/01 1
2 /2015/05/24/sqlite-tutorial.html 2018/04/01 1
3 /2016/11/07/coin-freq.html 2018/04/01 1
4 /2017/03/30/alpine-linux.html 2018/04/01 2
5 /2018/03/09/querySelectorAll.html 2018/04/01 1
6 /2017/11/24/fedora-27-rv.html 2018/04/01 1
> ggplot(livePostHit, aes(x = path, y = hits, fill = date)) +
geom_bar(stat='identity') +
theme(axis.text = element_text(angle=75, hjust = 1),
legend.position = 'none')
I turned off the legend because there were too many groups for it to render correctly, but I would like to create like a gradient for the highest to the lowest.

I just needed to make date an actual date type rather than a factor.
livePostHit$date <- as.Date(livePostHit$date)

Related

How to create a bar chart from the percentage of one variable when the X-axis is another variable?

I'm making a simple bar chart but I can't seem to figure it out. I've got my data as laid out here:
Candidate
SkinTone
Elected
1
7
1
2
4
0
3
3
0
4
2
1
Skin tone refers to a person's skin tone (obvs) and elected is a dummy variable that denotes whether a candidate was elected or not. What I want to do is have every skin tone value (it goes from 1-11) as a tick on my x-axis and my y-axis should be the percentage of those candidates that have a "1" as their elected value. So, for example, this tiny data set should generate a chart that looks like this:
Final Bar Graph
The problem I encounter is that I'm not able to figure out how to get this graph's y-axis correctly. Using this code below, I can generate a graph that looks like the one below:
ggplot(data=data, mapping = aes(x=Tone, y=Elected)) +
geom_bar(stat='identity',
fill="yellow",
col="black",
width=1,
alpha=.2) +
coord_cartesian(xlim = c(0.5,11.5)) +
scale_x_continuous(breaks = 1*1:11,
expand = expansion(add = .5)) +
labs(title="Skin Tone Electoral Success Barplot", x="Skin Tone", y="Percentage of Candidates Elected")
Incorrect Bar Graph
However, this doesn't work for me as the y-axis is showing the count of candidates who had a 1 in the Elected variable instead of the percentage. In addition, I'm getting these black blocks in between each observation, which I haven't gotten before when using col=. Lastly, I also find trouble adding in a density line as geom_density() gives me an error saying I'm missing my y aesthetic.

Plot frequency heatmap of positions from set of coordinates

I have a bunch of data that looks like this:
Track X1 X Y
1 Point 1 147.8333 258.5000
2 Point 2 148.5000 258.8333
3 Point 3 151.1667 260.8333
4 Point 4 154.5000 264.5000
5 Point 5 158.1667 266.5000
6 Point 6 161.5000 269.5000
I want to plot a heatmap of this, so a nice looking graph labelled x and y for the position coordinates, with a gradient color fill indicating the frequency that a particular point showed up, with a scale indicator showing what the colors mean. I'm looking for a simple gradient fill with a single color low and high.
I've been at this for a while but I think the first step should be to construct another data-set with the positions and a new column showing the frequencies? But I'm not 100% sure how to structure this.
So far my attempts look similar to:
ggplot(data=all_data, aes(x=X, y=Y)) + geom_tile(aes(fill=all_data$X)) +
scale_fill_gradient2(low="green", high="blue") + coord_equal()
As Jon Spring suggested, the following code shows up a graph like this:
all_data <- read.table(text = "
Track X1 X Y
1 Point 1 147.8333 258.5000
2 Point 2 148.5000 258.8333
3 Point 3 151.1667 260.8333
4 Point 4 154.5000 264.5000
5 Point 5 158.1667 266.5000
6 Point 6 161.5000 269.5000
", header = T, row.names = NULL)
ggplot(data=all_data, aes(x=X, y=Y)) + geom_bin2d()

ggplot2: facets: different axis limits and free space

I want to display two dimensions in my data, (1) reporting entity in different facets and (2) country associated to the data point on the x-axis. The problem is that the country dimension includes a "total", which is a lot higher than all of the individual values, so I would need an own axis limit for that.
My solution was to try another facetting dimension, but I could not get it working and looking nicely at the same time. Consider the following dummy data:
id <- c(1,1,1,1,1,1,2,2,2,2,2,2)
country <- c("US","US","UK","World","World","World","US","US","UK","World","World","World")
value <- c(150,40,100,1000,1100,1500,5,10,20,150,200,120)
# + some other dimensions
mydat <- data.frame(id,country,value)
id country value
1 1 US 150
2 1 US 40
3 1 UK 100
4 1 World 1000
5 1 World 1100
6 1 World 1500
7 2 US 5
8 2 US 10
9 2 UK 20
10 2 World 150
11 2 World 200
12 2 World 120
If I use a facet grid to display a world total, the axis limit is forced for the other countries as well:
mydat$breakdown <- mydat$country == "World"
ggplot(mydat) + aes(x=country,y=value) + geom_point() +
facet_grid(id ~ breakdown,scales = "free",space = "free_x") +
theme(strip.text.x = element_blank() , strip.background = element_blank(),
plot.margin = unit( c(0,0,0,0) , units = "lines" ) )
(the last part of the plot is just to remove the additional strip).
If I use a facet wrap, it does give me different axis limits for each plot, but then I cannot pass the space = "free_x" argument, meaning that the single column for the total will consume the same space as the entire country overview, which looks ugly for data sets with many countries:
ggplot(mydat) + aes(x=country,y=value) + geom_point() +
facet_wrap(id ~ breakdown,scales = "free")
There are several threads here which ask similar questions, but none of the answers helped me to achieve this yet.
Different axis limits per facet in ggplot2
Is it yet possible to have different axis breaks / limits for individual facets in ggplot with free scale?
Setting individual axis limits with facet_wrap and scales = "free" in ggplot2
Maybe try gridExtra::grid.arrange or cowplot::plot_grid:
lst <- split(mydat, list(mydat$breakdown, mydat$id))
plots <- lapply(seq(lst), function(x) {ggplot(lst[[x]]) +
aes(x=country,y=value) +
geom_point() +
ggtitle(names(lst)[x]) + labs(x=NULL, y=NULL)
})
do.call(gridExtra::grid.arrange,
c(plots, list(ncol=2, widths=c(2/3, 1/3)),
left="Value", bottom="country"))

special kind of bar plot in R

Imagine that I have the following matrix where every column corresponds to one bar.
1 1 3
1 3 1
1 2 2
I would like to make a stack bar plot where each number would determine a unit block of a different color.
For example if 1=red 2=blue and 3=green I would like to get the following result:
library(ggplot2)
library(reshape2)
chartset <- matrix(c(1,1,1,1,3,2,3,1,2), nrow = 3)
chartsetmelted <- data.frame(melt(chartset))
ggplot(chartsetmelted) + geom_tile(aes(x = Var2, y = Var1, fill = factor(value)), width = 0.8)
You can look at scale_fill_manual to specifically assign colors to values.

Set the width of ggplot geom_path based on a variable

I have two functions, a and b, that each take a value of x from 1-3 and produce an estimate and an error.
x variable estimate error
1 a 8 4
1 b 10 2
2 a 9 3
2 b 10 1
3 a 8 5
3 b 11 3
I'd like to use geom_path() in ggplot to plot the estimates and errors for each function as x increases.
So if this is the data:
d = data.frame(x=c(1,1,2,2,3,3),variable=rep(c('a','b'),3),estimate=c(8,10,9,10,8,11),error=c(4,2,3,1,5,3))
Then the output that I'd like is something like the output of:
ggplot(d,aes(x,estimate,color=variable)) + geom_path()
but with the thickness of the line at each point equal to the size of the error. I might need to use something like geom_polygon(), but I haven't been able to find a good way to do this without calculating a series of coordinates manually.
If there's a better way to visualize this data (y value with confidence intervals at discrete x values), that would be great. I don't want to use a bar graph because I actually have more than two functions and it's hard to track the changing estimate/error of any specific function with a large group of bars at each x value.
The short answer is that you need to map size to error so that the size of the geometric object will vary depending on the value, error in this case. There are many ways to do what you want like you have suggested.
df = data.frame(x = c(1,1,2,2,3,3),
variable = rep(c('a','b'), 3),
estimate = c(8,10,9,10,8,11),
error = c(4,2,3,1,5,3))
library(ggplot2)
ggplot(df, aes(x, estimate, colour = variable, group = variable, size = error)) +
geom_point() + theme(legend.position = 'none') + geom_line(size = .5)
I found geom_ribbon(). The answer is something like this:
ggplot(d,aes(x,estimate,ymin=estimate-error,ymax=estimate+error,fill=variable)) + geom_ribbon()

Resources