1807 escape of the Portuguese royal family and court from invading French forces
The Portuguese royal court transferred from Lisbon to the Portuguese colony of Brazil in a strategic retreat of Queen Maria I of Portugal, prince regent John, the Braganza royal family, its court, and senior officials, totaling nearly 10,000 people, on 27 November 1807. The embarkment took place on 27 November, but due to weather conditions, the ships were only able to depart on 29 November. The Braganza royal family departed for Brazil just days before Napoleonic forces invaded Portugal on 1 December 1807. The Portuguese crown remained in Brazil from 1808 until the Liberal Revolution of 1820 led to the return of John VI of Portugal on 26 April 1821.
For thirteen years, Rio de Janeiro functioned as the capital of the United Kingdom of Portugal, Brazil and the Algarves in what some historians call a metropolitan reversal (i.e., a colony exercising governance over the entirety of an empire). The period in which the court was located in Rio brought significant changes to the city and its residents, and can be interpreted through several perspectives. It had profound impacts on Brazilian society, economics, infrastructure, and politics. The transfer of the prince regent and the royal court "represented the first step toward Brazilian independence, since the prince regent immediately opened the ports of Brazil to foreign shipping and turned the colonial capital into the seat of government."
Background.
In 1807, at the outset of the Peninsular War, Napoleonic forces invaded Portugal due to the Portuguese alliance with Great Britain. The prince regent of Portugal at the time, John of Braganza, had formally governed the country on behalf of his mother, Queen Maria I of Portugal, since 1799. Anticipating the French invasion, John ordered the transfer of the Portuguese royal court to Brazil before he could be deposed. Setting sail for Brazil on 29 November 1807, the royal party navigated under the protection of the British Royal Navy, and eight ships of the line, five frigates, and four smaller vessels of the Portuguese Navy, under the command of Rear-Admiral Sir Sidney Smith. On 5 December, almost halfway between Lisbon and Madeira, Sidney Smith, along with Britain's envoy to Lisbon, Percy Smythe, 6th Viscount Strangford, returned to Europe with part of the British flotilla. Commodore Graham Moore continued escorting the Portuguese royal family to Brazil with the British ships "Marlborough", "London", "Bedford", and "Monarch".
Brazil was very sparsely populated, with a little over 3 million inhabitants. Around one-third of the colony's population consisted of enslaved peoples, most having been captured and shipped from Africa. The indigenous population at the time was of around 800,000 people, having been dramatically reduced and isolated during the first 300 years of exploration and colonization. Population density was concentrated along the Atlantic coastline. Rio de Janeiro, around the start of the 19th century, was experiencing a sizeable population boom. Over the 18th century, the population had increased tenfold due to the discovery of gold and diamonds and the migration of 800,000 individuals that ensued. In addition, it is estimated that 2 million enslaved Africans were brought to Brazil to work in mines and power the sugar industry. Brazilians were illiterate, poor, and lacking several basic needs, including medical care and public health services. Only 2.5% of free men were literate.
The transfer.
Arrival in Brazil and first transformations.
On 22 January 1808, Prince John and his court arrived in Salvador, Brazil. There, John signed a decree which opened Brazil's ports, allowing commerce between Brazil and "friendly nations". This was particularly beneficial for Great Britain and can be seen as one of many ways Prince John found to reward Britain for its assistance. This new law, however, broke the colonial pact that had forced Brazil to maintain direct commercial relations with Portugal only. This transformed the Brazilian economy, and subsequently, its demographics and society. Secret negotiations at London in 1807 by Portuguese ambassador Domingos António de Sousa Coutinho guaranteed British military protection in exchange for British access to Brazil's ports and to Madeira as a naval base. Sousa Coutinho's secret negotiations paved the way for John's law to come to fruition in 1808. 
On 1 April 1808, in attempts to modernize the economy and diversify the production of the colony, John allowed for the establishment of manufacturing industries through the signing of the "Alvará de Liberdade para as Indústrias". This meant that Brazil would no longer only be an agricultural producer. In this decree, John said that in an attempt to promote national wealth and recognize that manufacturing, industrial labor, and multiplication of labor promote means of subsistence for subjects, Brazil should heavily invest in those sectors effective immediately. He abolished any prohibition to industrial development; which expanded the need for labor in the colony, along with attracting foreign investment.
The court in the colony.
Between 1808 and 1821, John of Braganza, serving as prince regent until 1816 and then as king from 1816 onwards, granted 145 nobility titles. During the time that the court was located in Brazil, the Portuguese royal family collectively granted more titles of nobility than it had in its past 300 years of existence in Portugal. Much can be said about the motivations for ennobling so many people, and these titles had consequences to the political scene of Brazil, including the systematic isolation of Brazilians from politics. Between 1811 and 1821, a vast majority of noble titles were granted to those who had travelled with the court in 1807 or had fought the French in Portugal and somehow had made their way to Brazil. 
As an additional way to thank Great Britain for their efforts to protect the Portuguese Empire and their expanding economic relationship with the colony, titles of nobility were also given to British individuals. Furthermore, the titles of nobility served as a means to consolidate the rule of the Portuguese court and confirm the power status of the monarchy in the colony. When John elevated the status of Brazil from colony to a co-kingdom as the United Kingdom of Portugal, Brazil and the Algarves to participate in the Congress of Vienna away from Europe, there was a sharp increase in the number of titles granted. Not only did this change affect nobility titles, it also increased the power of the Portuguese in Brazilian affairs. Rio became an important center for world trade and grew immensely, having its demographics changed incessantly. The monarchy, as expected, favored the Portuguese to be in command of political offices, and with the creation of new government positions, departments, and military branches, almost every official was Portuguese.
Out of all 145 nobility titles granted during this time period, only six were granted to Brazilians. Consistently, Brazilians were given the lowest royal title, that of baron. To somewhat make up for the fact that the Portuguese not only got more titles but also got more prestigious titles that made them more influential with the nobility, Brazilians were also granted land and seats in the "Conselho da Fazenda". These were surreptitious ways to keep Brazilians content with the monarchy and appease the population without jeopardizing Portuguese high society, both in Brazil and in Portugal. The first title of nobility granted to a Brazilian was in 1812 to the Baroness of São Salvador de Campos dos Goytacazes. Out of the 26 titles of nobility granted in 1818, only three Brazilian men were graced: José Egídio Álvares de Almeida, Pedro Dias Paes Leme, and Paulo Fernandes Carneiro Viana. 
The reason so many noble titles were granted in 1818 is most likely because that was the year John was crowned king, with 17 noble titles being granted on the day of his coronation. As it can be expected, these titles did not benefit Brazilians much and did not create tight bonds between Brazilians and the new nobility. It was a reality unknown to many, even the wealthiest Brazilians. An argument can be made of this: nobility titles were made exclusively for Europeans to preserve the contrast in power and superiority of Europeans in Brazil. By granting titles to Portuguese individuals and those with close ties with Portugal, the court guaranteed the financial support to sustain themselves halfway across the Atlantic.
Importantly, this meant an increase in the demand for slave labor. With the end of the colonial pact, the coffee industry boomed and the sugar industry continued to flourish. Now, being able to manufacture goods, the naval and iron industries started to develop. The arrival of enslaved individuals increased dramatically during the period that the court was in Brazil and then during the decade following their absence, with the arrival of approximately 328,000 enslaved individuals to Brazil. This drastically changed the demographics of Brazil and of Rio, where most of these enslaved individuals would arrive and remain. It is estimated that the enslaved population in Rio, at its height, was more than half of the total population. After the successful slave revolution that took place in Haiti a few years before, the court started to worry about the small elites regarding potential rebellion and revolution. This led to the creation of the Military Division of the "Guarda Real de Polícia", or Royal Police Guard, in charge of urban policing that before the arrival of the royal family consisted of informal guards, watchmen, and sentinels. This further isolated and oppressed enslaved peoples and was the beginning of a phenomenon that proceeded in the 19th and 20th centuries of the criminalization of poverty. It reemphasized racial discrimination on an official level and associated disorder with lack of money and social ascension. This is also when King John VI decreed the establishment of a mounted guard. 
In addition, the penal system was used to take control of lower classes by using minor infractions considered public disorder; for example, "disrespecting curfew, playing games of luck, drinking alcohol and begging" could be punishable with prison. Furthermore, while attempts to "civilize" the city were made, it also meant that the biggest difference between the old court and the one in Brazil was that half of it now consisted of enslaved peoples. Slavery was not legal in Portugal but allowed in the New World, and continued for several decades even after Brazil achieved independence from Portugal. 
Imperial relocation also meant the stratification of hierarchy. Those who were already rich, usually because of their connections to nobility, got richer (usually for the same reasons they had been rich in the first place) and the poorer got even poorer, now having to compete for resources, services, and physical space. With the Portuguese government now in Brazil, Portuguese immigration retention increased and this led to further disapproval of Cariocas (the term given to those native to the city of Rio de Janeiro. While the court and nobility wanted to portray itself as open to hearing the critiques and desires of the Brazilian population, only a select few could attend audiences with King John. He implemented the ceremony of "beija-mão", a daily ritual where subjects got the chance to go to the royal residence, kiss the king's hand, and express their grievances. This practice to supposedly stay in touch with common people allowed for the social elites to voice their agendas, including white men, the nobility, and the clergy.
Brazil as a kingdom.
On 16 December 1815, John created the United Kingdom of Portugal, Brazil and the Algarves ("Reino Unido de Portugal, Brasil e Algarves"), elevating Brazil to the same rank as Portugal and increasing the administrative independence of Brazil. Brazilian representatives were elected to the Portuguese Constitutional Courts ("Cortes Constitucionais Portuguesas"). In 1815, in the aftermath of Napoleon's defeat and the meeting of the Congress of Vienna, convened to restore European political arrangements, the Portuguese monarch declared Brazil a co-equal to Portugal to increase Portugal's bargaining power. In 1816, with the death of Queen Maria, Prince John became king of the United Kingdom of Portugal, Brazil and the Algarves. After several delays, the ceremony of his acclamation took place in Rio de Janeiro in 1818.
Beyond having to go through infrastructural expansion to accommodate for the arrival of 15,000 people, Rio continued to be modified and upgraded in the early stages of the transferring of the court. The city lacked basic sanitation and structured sewer systems. There were very few roads. The goal was to “construct an ideal city; a city in